Can someone help me out and tell me how to chance this code to validate alpha-numeric? Really appreciate your help!! function isValidCode(field) { var valid = "0123456789+"; for (var i=0; i < field.length; i++) { temp = field.substring(i, i+1); if (valid.indexOf(temp) == "-1") { alert("Invalid post code.Please try again."); return false; } } return true; }
It might work. BTW, indexOf() returns -1, and not "-1". There is much shorter version using regular expressions: function isValidCode(field) { return field.search(/^[0-9+]*$/)!=-1; } Code (markup):
Thanks for your reply. I am not exactly great with Java and this has me stumped! THe code checks a field and if the field entry is anything but numbers it returns an error. OX28 4BS returns an error 11630 is fine I need it to allow text and numbers OX28 4BS No Error 11630 No Error
It's only a matter of changing the regex: function isValidCode(field) { return field.search(/^[0-9a-zA-Z]*$/)!=-1; } Code (markup): Just put inside the square brackets ([]) the range of characters you'd like to accept.