regex - JavaScript - check for numbers in a text field -


it takes in value of textfield, im trying have if numbers in field il able stop it.

 function hasnumbers(input) {       var length = input.length;      var end = 0;       (var x = 0; x < length; x++) {          var char = input.substring(x, 1);          truefalse = isnan(char);          if (! truefalse) {              end++;          }      }       if (end > 0) {          return false;      }       return true;  } 

you can replace function this:

function hasnumbers(input) {     return /[0-9]/.test(input); } 

/[0-9]/ constructs regular expression matches numeric character.

once have regex object, can call test(s) see if s matches regex.

here more information regexp.test() method.

here more info on regular expressions in general.

here fiddle shows above code in action.

here another fiddle shows above function validating text input.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -