Can anyone tell me why does this not work for integers but works for characters? I really hate reg expressions since they are cryptic but will if I have too. Also I want to include the "-()" as well in the valid characters.
String.prototype.Contains = function (str) {
return this.indexOf(str) != -1;
};
var validChars = '0123456789';
var str = $("#textbox1").val().toString();
if (str.Contains(validChars)) {
alert("found");
} else {
alert("not found");
}
The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.
The indexOf() method returns the position of the first occurrence of substring in string. The first position in the string is 0. If the indexOf() method does not find the substring in string, it will return -1.
Yes, indexOf() works with array (of number, string, or any object), as well as with string.
indexOf(v) instead, where ~ is the JavaScript bitwise NOT operator.
Review
String.prototype.Contains = function (str) {
return this.indexOf(str) != -1;
};
This String "method" returns true if str
is contained within itself, e.g. 'hello world'.indexOf('world') != -1would return
true`.
var validChars = '0123456789';
var str = $("#textbox1").val().toString();
The value of $('#textbox1').val()
is already a string, so the .toString()
isn't necessary here.
if (str.Contains(validChars)) {
alert("found");
} else {
alert("not found");
}
This is where it goes wrong; effectively, this executes '1234'.indexOf('0123456789') != -1
; it will almost always return false
unless you have a huge number like 10123456789
.
What you could have done is test each character in str
whether they're contained inside '0123456789'
, e.g. '0123456789'.indexOf(c) != -1
where c
is a character in str
. It can be done a lot easier though.
Solution
I know you don't like regular expressions, but they're pretty useful in these cases:
if ($("#textbox1").val().match(/^[0-9()]+$/)) {
alert("valid");
} else {
alert("not valid");
}
Explanation
[0-9()]
is a character class, comprising the range 0-9
which is short for 0123456789
and the parentheses ()
.
[0-9()]+
matches at least one character that matches the above character class.
^[0-9()]+$
matches strings for which ALL characters match the character class; ^
and $
match the beginning and end of the string, respectively.
In the end, the whole expression is padded on both sides with /
, which is the regular expression delimiter. It's short for new RegExp('^[0-9()]+$')
.
Assuming you are looking for a function to validate your input, considering a validChars
parameter:
String.prototype.validate = function (validChars) {
var mychar;
for(var i=0; i < this.length; i++) {
if(validChars.indexOf(this[i]) == -1) { // Loop through all characters of your string.
return false; // Return false if the current character is not found in 'validChars' string.
}
}
return true;
};
var validChars = '0123456789';
var str = $("#textbox1").val().toString();
if (str.validate(validChars)) {
alert("Only valid characters were found! String validates!");
} else {
alert("Invalid Char found! String doesn't validate.");
}
However, This is quite a load of code for a string validation. I'd recommend looking into regexes, instead. (Jack's got a nice answer up here)
You are passing the entire list of validChars
to indexOf()
. You need to loop through the characters and check them one-by-one.
Demo
String.prototype.Contains = function (str) {
var mychar;
for(var i=0; i<str.length; i++)
{
mychar = this.substr(i, 1);
if(str.indexOf(mychar) == -1)
{
return false;
}
}
return this.length > 0;
};
To use this on integers, you can convert the integer to a string with String()
, like this:
var myint = 33; // define integer
var strTest = String(myint); // convert to string
console.log(strTest.Contains("0123456789")); // validate against chars
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With