1
$(document).ready(function(){
    $("#userid").blur(function(){
        $.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
            if(data == "blank"){
                $("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
            }
        });

    });
});

大家好,我有这段代码使用 jQuery 来验证用户 ID。将会有更多的验证,例如密码、名称等。但我很困惑。现在我可以告诉用户插入的用户 ID 包含错误。在发布表格时,我如何检查错误?

例如,userid 和 name 出错,所以当我们点击提交时,表单不会提交。如果没有显示错误,则可以提交表单。

提前致谢!

4

3 回答 3

0
<script type="text/javascript">
/*$(document).ready(function(){
    $("#userid").blur(function(){
        $.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
            if(data == "blank"){
                $("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });

    });
     $("#useremail").blur(function(){
        $.post("validation.php?input=useremail",{ useremail:$("#useremail").val() } ,function(data){
            if(data == "blank"){
                $("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });

    });
     $("#username").blur(function(){
        $.post("validation.php?input=username",{ username:$("#username").val() } ,function(data){
            if(data == "blank"){
                $("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });

    });
});*/

function validate_userid(){
    $.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
            if(data == "blank"){
                $("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });
}
function validate_useremail(){
    $.post("validation.php?input=useremail",{ useremail:$("#useremail").val() } ,function(data){
            if(data == "blank"){
                $("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });
}

function validate_username(){
    $.post("validation.php?input=username",{ username:$("#username").val() } ,function(data){
            if(data == "blank"){
                $("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });
}
function validate(){
var userid = $("#userid").val();
var useremail = $("#useremail").val();
var username = $("#username").val();
if(userid == ""){
    validate_userid();
}else if(useremail == ""){
    validate_useremail();
}else if(username == ""){
    validate_username();
}
}

/*You can use any one of the validate()--------------the two are working as same-------------------*/
function validate(){
    var userid = $("#userid").val();
    var useremail = $("#useremail").val();
    var username = $("#username").val();
    if(userid == ""){
        $.post("validation.php?input=userid",{ userid:$("#userid").val() } ,function(data){
            if(data == "blank"){
                $("#useridError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });
    }else if(useremail == ""){
        $.post("validation.php?input=useremail",{ useremail:$("#useremail").val() } ,function(data){
            if(data == "blank"){
                $("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });
    }else if(username == ""){
        $.post("validation.php?input=username",{ username:$("#username").val() } ,function(data){
            if(data == "blank"){
                $("#useremailError").removeClass().addClass('messageError').html('Please do not leave blank!');
                return false;
            }
        });
    }
}

</script>
<form action="" method="post">
<input type="text" name="userid" id="userid" value="" onblur="return validate_userid();" />
<div id="useridError"></div>

<input type="text" name="useremail" id="useremail" value="" onblur="return validate_useremail();" />
<div id="useremailError"></div>

<input type="text" name="username" id="username" value="" onblur="return validate_username();" />
<div id="usernameError"></div>

   <input type="submit" value="SUBMIT" name="submit" id="submit" onclick="return validate();" />
</form>
于 2012-11-28T04:04:19.797 回答
0

Regex您可以使用如下方式检查值

脚本

var ck_name = 'YourDesiredRegExForValidation';//set your desired Regex to validate your `name`

function validate(form){
   var name = form.name.value;
   //set your all field's value in a variable
   var errors = [];

 if (!ck_name.test(name)) {
  errors[errors.length] = "You valid Name .";
 }
 //simultaneously check out all fields

 if (errors.length > 0) {
      reportErrors(errors);
      return false;
 }
  return true;
}

// Following function is just to display Errors
function reportErrors(errors){
      var msg = "Please Enter Valide Data...\n";
      for (var i = 0; i<errors.length; i++) {
              var numError = i + 1;
              msg += "\n" + numError + ". " + errors[i];
      }
      alert(msg);
 }

看看这个

于 2012-11-28T04:04:27.160 回答
0

   简而言之,如果数据没有通过验证,让函数返回false;返回 false 将阻止表单将数据发布到服务器;如果数据确实通过了验证,则让函数返回 true。
   我可以进一步解释,但基本上,这就是需要发生的事情。

   希望这可以帮助。

于 2012-11-28T04:42:24.103 回答