October 13, 2012
Check whether an input string contains number
Question by Udara S.S Liyanage
I want to check whether an input string contains number.
I am trying to validate an input field. I want it to contain only alphabet, not numbers.
Answer by Starx
You can do this using javascript. No need for Jquery or Regex
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
While implementing
var val = $('yourinputelement').val();
if(isNumeric(val)) { alert('number'); }
else { alert('not number'); }
Update: To check if a string has numbers in them, you can use regular expressions to do that
var matches = val.match(/d+/g);
if (matches != null) {
alert('number');
}