Reputation: 20545
Now i have a textfield and in javascript i wish to check if the value of this textfield is or contains a String.
I have tried the following:
if(isNaN(parseInt($(this).val()))){}
Without effect.
ive also tried:
if(typeof(parseInt($(this).val())) === "string"){}
ALTHOUGH these two examples might seem to work. They don't if you simply put the first character as a number then the rest of it wont matter.
So my question is how ?
Upvotes: 0
Views: 158
Reputation: 41
if(typeof($(this).val()) == "string"){//is a string
//do something with string
}else if(typeof($(this).val()) == "object"){//is a object
if($(this).val().length != undefined){//is a array
//do something with array
}else{//is a object
//do something with json object
}
}
Upvotes: 0
Reputation: 19573
if(!isNaN(parseInt($(this).val())) && $(this).val().length==parseInt($(this).val()).length) {}
Upvotes: 1
Reputation: 173
You can split the string into a character array and check each individually:
var s = "11test";
var split_s = s.split("");
for(var index=0;index<split_s.length;index++) {
if(isNaN(parseInt(split_s[index]))){
}
}
Upvotes: 1