猿问

将数据插入数据库表的条件

Js 新手并卡在这里。我应该在 if 语句中放入什么来将数据插入数据库表?因为现在当我提交错误的值但它仍然将它插入到数据库时,我确实收到了预期的错误消息。感谢帮助!这是我的 index.js


    var manageMemberTable;

    $("#addMemberModalBtn").on('click', function() {

        // reset the form 

        $("#createMemberForm")[0].reset();

        // remove the error 

        $(".form-group").removeClass('has-error').removeClass('has-success');

        $(".text-danger").remove();

        // empty the message div

        $(".messages").html("");


        // submit form

        $("#createMemberForm").unbind('submit').bind('submit', function() {


            $(".text-danger").remove();


            var form = $(this);


            // validation

            var firstname = $("#firstname").val();

            var lastname = $("#lastname").val();

这就是我要检查的


    if (firstname == "") {

                $("#firstname").closest('.form-group').addClass('has-error');

                $("#firstname").after('<p class="text-danger">The firstname field is required</p>');

            }

            else {


            if (firstname.match(/^[a-zA-Z ]+$/) === null){

                $("#firstname").closest('.form-group').addClass('has-error');

                $("#firstname").after('<p class="text-danger">Firstname invalid</p>');

            }

            else {

                $("#firstname").closest('.form-group').removeClass('has-error');

                $("#firstname").closest('.form-group').addClass('has-success');

            }   

            }           



                //lastname validation


                if (lastname == "") {

                    $("#lastname").closest('.form-group').addClass('has-error');

                    $("#lastname").after('<p class="text-danger">The lastname field is required</p>');

                }


慕无忌1623718
浏览 224回答 1
1回答

慕的地6264312

这可以是一种方法。firstname.match(/^[a-zA-Z ]+$/)true如果字符串与正则表达式匹配,则返回if ((firstname != "") && (firstname.match(/^[a-zA-Z ]+$/)) &&&nbsp; &nbsp; (lastname != "") && (lastname.match(/^[a-zA-Z ]+$/))) {&nbsp; &nbsp; $("#firstname").closest('.form-group').addClass('has-success');&nbsp; &nbsp; $("#lastname").closest('.form-group').addClass('has-success');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // code to send data to database}else {&nbsp; &nbsp; if(firstname === ""){&nbsp; &nbsp; &nbsp; &nbsp; $("#firstname").closest('.form-group').addClass('has-error');&nbsp; &nbsp; &nbsp; &nbsp; $("#firstname").after('<p class="text-danger">The firstname field is required</p>');&nbsp; &nbsp; }&nbsp; &nbsp; if (!firstname.match(/^[a-zA-Z ]+$/)){&nbsp; &nbsp; &nbsp; &nbsp; $("#firstname").closest('.form-group').addClass('has-error');&nbsp; &nbsp; &nbsp; &nbsp; $("#firstname").after('<p class="text-danger">firstname is invalid</p>');&nbsp; &nbsp; }&nbsp; &nbsp; if(lastname === ""){&nbsp; &nbsp; &nbsp; &nbsp; $("#lastname").closest('.form-group').addClass('has-error');&nbsp; &nbsp; &nbsp; &nbsp; $("#lastname").after('<p class="text-danger">The lastname field is required</p>');&nbsp; &nbsp; }&nbsp; &nbsp; if (!lastname.match(/^[a-zA-Z ]+$/)){&nbsp; &nbsp; &nbsp; &nbsp; $("#lastname").closest('.form-group').addClass('has-error');&nbsp; &nbsp; &nbsp; &nbsp; $("#lastname").after('<p class="text-danger">lastname is invalid</p>');&nbsp; &nbsp; }&nbsp;&nbsp;}&nbsp;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答