Validation methods submitted (compared by a single character):
<! DOCTYPE html>verifydate<form id= "Form1" action= "vefifydata.html" > <input type= "text" name= "D1"/> <input type= "Submit"/> ; </form> <script type= "Text/javascript" >document.getElementById ("Form1"). onsubmit =function Chkform () {var text= This. D1.value; alert (text.length); for(i=0;i<text.length;i++) {alert (i+ "The character is" +Text.charat (i)); if(Text.charat (i) > ' 9 ' | | Text.charat (i) < ' 0 ') {alert ("First" + i + "non-numeric characters" +Text.charat (i)); } Else{alert ("First" + i + "Numeric characters" +Text.charat (i)); } } return false; } </script></body>Commit Validation method (via regular expression)
JS with pattern.test (need to verify string) pattern is regular expression
<! DOCTYPE html>verifydate<form id= "Form1" action= "vefifydata.html" > <input type= "text" name= "D1"/> <input type= "Submit"/> ; </form> <script type= "Text/javascript" >document.getElementById ("Form1"). onsubmit =function Chkform () {var text= This. D1.value; if(Verifydata (text)) {alert ("All numbers ..."); return false; }Else{alert ("Not all numbers ..."); return false; }; } function Verifydata (text) {var datapattern=/^\d[0-9]{0,}$/; returndatapattern.test (text); } </script></body>Methods to validate before committing
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Insert Title here</title></Head><Body>verifydate<formID= "Form1"Action= "Http://www.cnblogs.com/qingyundian"onsubmit= "return Chkform ()"> <inputtype= "text"ID= "D1" /> <inputtype= "Submit" /> </form> <Scripttype= "Text/javascript"> functionChkform () {vartext=document.getElementById ("D1"). Value; alert (text); if(Verifydata (text)) {alert ("all the numbers ..."); Alert ("verify successful, then commit ..."); return true; }Else{alert ("not all for numbers ..."); Alert ("checksum failed, no commit"); return false; } return false; } functionVerifydata (text) {varDatapattern=/^\d[0-9]{0,}$/; returndatapattern.test (text); } </Script></Body></HTML>
This method will first verify
Verification success will return true to OnSubmit
Validation failure will return false to OnSubmit
Verify successful Open activation webpage
Java Html&javascript interview question: How do I verify that the contents of a numeric text box are all numbers before the Html form is submitted? Otherwise, prompt the user and terminate the submission?