Home > Web Front-end > JS Tutorial > JavaScript form validation study notes

JavaScript form validation study notes

PHPz
Release: 2017-04-04 14:11:39
Original
1280 people have browsed it

JavaScript Form Validation

JavaScript can be used to validate the input data in the HTML form before the data is sent to the server.
Form data often requires the use of JavaScript to verify its correctness:

Verify whether the form data is empty?

Verify that the input is a correct email address?

Verify whether the date is entered correctly?

Verify whether the form input content is numeric?

Required (or required) items
The following function is used to check whether the user has filled in the required (or required) items in the form. If required or the required option is empty, a warning box will pop up and the return value of the function is false, otherwise the return value of the function is true (meaning there is no problem with the data) :

    function validateForm(){ 
    var x=document.forms["myForm"]["fname"].value; 
    if (x==null || x=="")
    { alert("姓必须填写"); 
    return false;
    }}
Copy after login

E-mail Validation
The following function checks whether the entered data conforms to the basic syntax of an email address.
means that the input data must contain the @ symbol and the period (.). At the same time, @ cannot be the first character of the email address, and there must be at least one period after @:

        function validateForm(){
            var x=document.forms["myForm"]["email"].value;
            var atpos=x.indexOf("@");
            var dotpos=x.lastIndexOf(".");
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
                alert("不是一个有效的 e-mail 地址");
                return false;
            }
        }
Copy after login

Today’s exercise was not well written and I didn’t have time to refine it, so I posted the code directly

        <!DOCTYPE html>
        <html>
            <head>
                <meta charset="UTF-8">
                <title>登录页面</title>
            <link rel="stylesheet" href="C:\Users\Administrator\Desktop\bootstrap-3.3.5-dist\css\bootstrap.min.css" />
            <script type="text/javascript" src="dse.js" ></script>
        <!--      <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> -->
        <!--    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> -->
            </head>
        <body>
            <p class="p1">
            <p class="p0">
        <form class="form-horizontal" role="form">
            <table >a
            <tr>
                <td align=&#39;right&#39;><label>用户名:    </label></td>
                <td ><input type="text" class="form-control" id=&#39;user&#39; " /></td>
                <td align=&#39;right&#39; id="worr1" width="150px"></td>
            </tr>
            <tr>
                <td align=&#39;right&#39;><label> 昵称:    </label></td>
                <td ><input type="text" class="form-control" id="name"/></td>
                <td align=&#39;right&#39; id="worr2" ></td>
            </tr>
            <tr>
                <td align=&#39;right&#39;><label>密码:    </label></td>
                <td ><input type="password" class="form-control" id="pwd1" /></td>
                <td align=&#39;right&#39; id="worr3"></td>
            </tr>        
            <tr>
                <td align=&#39;right&#39;><label>确认密码:    </label></td>
                <td><input type="password" class="form-control" id="pwd2" /></td>
                <td align=&#39;right&#39; id="worr"></td>
            </tr>
            <tr>
                <td align=&#39;right&#39;><label>邮箱:    </label></td>
                <td > <input type="text" class="form-control" id="email" /></td>
                <td align=&#39;right&#39; id="worr4" ></td>
            </tr>
            <tr>
                <td align=&#39;right&#39;><label>手机号:    </label></td>
                <td ><input type="text" class="form-control" id="phone" /></td>
                <td align=&#39;right&#39; id="worr5" ></td>
            </tr>
            <tr>
                <td  colspan="3"><a href="#" id="a1">用户手册</a></td>
            </tr>
            <tr>
                <td  colspan="3"><button onclick ="but1()" class="btn btn-default"><label >提交</label></button></td>
                <td align=&#39;right&#39; id="worr6" ></td>
            </tr>
            </table>

        </form>

        </p>
            </body>
        </html>
Copy after login

js

    function but1() {
        but2();
        but3();
        but4();
        but5();
        but6();
    }
    function but2() {
        var pwd1=document.getElementById('pwd1').value;
        var pwd2=document.getElementById('pwd2').value;

            if (pwd1=="") {
            document.getElementById('worr3').innerHTML="密码不能为空";
            }
            if (pwd2=="") {
            document.getElementById('worr').innerHTML="密码不能为空";
            }
            try{
                if (pwd1===pwd2) {

                }else{
                        throw '密码不一致';            
                }
            }catch(err){
                document.getElementById('worr').innerHTML=" "+ err +" ";
                // document.getElementById('worr').style.color='red';

            }

    }
    function but3() {
        var user=document.getElementById('user').value;
        if (user=="") {
            document.getElementById('worr1').innerHTML="用户名不能为空";
            }
        var name=document.getElementById('name').value;
        if (name=="") {
            document.getElementById('worr2').innerHTML="昵称不能为空";
            }
    }
    function but4() {
        var email=document.getElementById('email').value;
        if (email=="") {
        document.getElementById('worr4').innerHTML="邮箱不能为空";
        }else{
        var atpos=email.indexOf("@");
        var dotpos=email.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length){
            document.getElementById('worr4').innerHTML="不是一个有效的 e-mail 地址";
        }

        }
    }
    function but5() {

        var phone=document.getElementById('phone').value;
        if (phone=="") {
        document.getElementById('worr5').innerHTML="手机号不能为空";
        }else{
     var reg = /^0?1[3|4|5|8][0-9]\d{8}$/;
     if (reg.test(phone)) {
          alert("号码正确~");
     }else{
              document.getElementById('worr5').innerHTML="号码有误~";
     };

        }
    }
    function but6() {
            var user=document.getElementById('user').value;
            var name=document.getElementById('name').value;
            var pwd2=document.getElementById('pwd2').value;
            var email=document.getElementById('email').value;
            var phone=document.getElementById('phone').value;
              document.getElementById('worr6').innerHTML=" "+user+"  "+name+"  "+pwd2+"  "+email+"  "+phone;
    }
Copy after login

The above is the detailed content of JavaScript form validation study notes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template