How do I use a submit function to ensure that a name field only has letters, zip code field only has numbers, etc. w/ Javascript when submitting a form?
kahaj 0 Junior Poster
Recommended Answers
Jump to Postuse the concept at this url
http://www.w3schools.com/js/js_form_validation.asp
in conjunction with this url
http://developer.apple.com/internet/webcontent/validation.html
Jump to Postthis is for checking only numbers:
function Only_Num(id) { if(isNaN(document.getElementById(id).value)) { alert("Please Enter Only Numbers (0-9).."); document.getElementById(id).value=""; document.getElementById(id).focus(); } return; }
this for only letters:
function isAlphabet(elem, helperMsg){ var alphaExp = /^[a-zA-Z]+$/; if(elem.value.match(alphaExp)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } }
Jump to PostNo, both your usages of isNaN won't work since they would also work for input such as "3E3". A better way would be to make sure that your form field only contains dots and digits. Also in both the cases, there is an implicit conversion from Javscript String object to …
All 11 Replies
R0bb0b 344 Posting Shark
kahaj 0 Junior Poster
kahaj 0 Junior Poster
kahaj 0 Junior Poster
TnWbDzgnr1996 0 Newbie Poster
kahaj 0 Junior Poster
Shanti C 106 Posting Virtuoso
Shanti C 106 Posting Virtuoso
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
krishikrishi1 0 Newbie Poster
TnWbDzgnr1996 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.