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");
}
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");
}
Share
Improve this question
edited Dec 18, 2012 at 8:00
Cerbrus
72.9k19 gold badges136 silver badges150 bronze badges
asked Dec 18, 2012 at 7:55
FabFab
9542 gold badges15 silver badges38 bronze badges
6
-
Have you tried casting it to a string?
(string)intVar
Edit: That probably doesn't work in javascript. – qwerty Commented Dec 18, 2012 at 7:58 -
1
This function only returns
'found'
, if your textbox contains'0123456789'
somewhere. What exactly are you trying to do? – Cerbrus Commented Dec 18, 2012 at 7:59 - If you want to make it work for integers, you have add Number.prototype.Contains as well – Chris Li Commented Dec 18, 2012 at 8:01
-
@ChrisLee:
str
is already cast.toString();
, so that shouldn't be necessary – Cerbrus Commented Dec 18, 2012 at 8:02 - If you are trying to use this validations, I would remend using regex as opposed to the contains . – Ravi Y Commented Dec 18, 2012 at 8:02
5 Answers
Reset to default 4Review
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, prising 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()]+$')
.
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
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 remend looking into regexes, instead. (Jack's got a nice answer up here)
I'm only guessing, but it looks like you are trying to check a phone number. One of the simple ways to change your function is to check string value with RegExp.
String.prototype.Contains = function(str) {
var reg = new RegExp("^[" + str +"]+$");
return reg.test(this);
};
But it does not check the sequence of symbols in string.
Checking phone number is more plicated, so RegExp is a good way to do this (even if you do not like it). It can look like:
String.prototype.ContainsPhone = function() {
var reg = new RegExp("^\\([0-9]{3}\\)[0-9]{3}-[0-9]{2}-[0-9]{2}$");
return reg.test(this);
};
This variant will check phones like "(123)456-78-90"
. It not only checks for a list of characters, but also checks their sequence in string.
Thank you all for your answers! Looks like I'll use regular expressions. I've tried all those solutions but really wanted to be able to pass in a string of validChars but instead I'll pass in a regex..
This works for words, letters, but not integers. I wanted to know why it doesn't work for integers. I wanted to be able to mimic the FilteredTextBoxExtender from the ajax control toolkit in MVC by using a custom Attribute on a textBox