Javascript Form validation | set a specific input format -
i'm working on project has input field requiring user enter of following 3 options:
- number 150
- number starting 1 letter (which must n, not case sensitive) n150
- number ending 1 letter (which must n, not case sensitive) 150n
any other value like:
- 150x return error message
wrong input - x150 return
wrong input - 1n50 return
wrong position
the correct way make array of valid numbers , check if given text exists on array.
for example:
var validnumbers = [ 150, n150, 150n ]; if (validnumbers.indexof(parseint(num, 10)) >=0 ) { //match } you'll need indexof function ie:
if (!array.prototype.indexof) { array.prototype.indexof = function(needle) { for(var = 0; < this.length; i++) { if(this[i] === needle) { return i; } } return -1; }; }
Comments
Post a Comment