使用jquery检查对文本框的验证

时间:2014-03-18 12:35:21

标签: php jquery

我有一些文本框,它们的值是从某个源生成的。如果我删除了值,那么编辑它们然后我点击提交按钮我的jquery验证不起作用 我正在使用这个jquery。

$(function() {$("#error1").hide();});

$('#save').click(function() { 
         
     $('input:text').each(function(){
       if( $(this).val().length == 0)
          $("#error1").show('slow');
  
    });
});

通过这个jquery我想验证我的所有文本框。即使我的必填字段验证器不工作。这可能是一个老问题,但我试图找到解决方案,但我没有得到任何有效的答案。请帮助。

4 个答案:

答案 0 :(得分:0)

试试这个,

$(function() {
    $("#error1").hide();
    $('#save').click(function() { // write this in document.ready function
        var flag=false;
        $('input:text').each(function(){
           if( $(this).val().length == 0) {
              flag=true;
              return false; //break the loop if showing common message
           }
        });
        if(flag === true) {// show error once only
             $("#error1").show('slow');
        }
    });
});

答案 1 :(得分:0)

尝试通过此链接提供的示例,您将获得一个想法

http://www.jquerybyexample.net/2012/12/jquery-to-check-all-textboxes-empty.html

答案 2 :(得分:0)

检查此 Demo jsFiddle

JQuery的

$("#error1").hide(); 
$("#save").off().on('click', function(){   
    if($('#txtname').val().length == 0){                      
        $("#error1").show('slow');
        return false;
    } else {
        $("#error1").hide();    
    }
});

HTML

<input type="text" id="txtname" name="txtname">
<input type="submit" id="save" />
<p id="error1">Textbox can not be blank.!</p>

答案 3 :(得分:0)

你可以尝试这个jsFiddle http://jsfiddle.net/pM4wL/

<强>脚本

$(function() {$("#error1").hide();});

$('#save').click(function() {
     validInput = true;
     $('input:text').each(function(){
         if($(this).val().length === 0) {
            validInput = false;
            $(this).addClass("input-error");
            $("#error1").show('slow');
         }
    });
    if(validInput) {
        $("#myForm").submit();
    }
});