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?

Source: Internet
Author: User
Tags html form

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"/&gt    ; </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"/&gt    ; </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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.