Jquery表单提交以检查空字段

时间:2013-05-15 04:37:32

标签: jquery form-submit

如何在不加载login.php的情况下提交时,如何使用jquery检查文本字段是否为空?

<form action="login.php" method="post">
    <label>Login Name:</label>
    <input type="text" name="email" id="log" />
    <label>Password:</label>
    <input type="password" name="password" id="pwd" />
    <input type="submit" name="submit" value="Login" />
</form>

感谢。

8 个答案:

答案 0 :(得分:39)

你可以这样做:

//  Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {

    // Get the Login Name value and trim it
    var name = $.trim($('#log').val());

    // Check if empty of not
    if (name  === '') {
        alert('Text-field is empty.');
        return false;
    }
});

FIDDLE DEMO

答案 1 :(得分:10)

您可以使用'required'http://jsbin.com/atefuq/1/edit

<form action="login.php" method="post">
    <label>Login Name:</label>
    <input required type="text" name="email" id="log" />
    <label>Password:</label>
    <input required type="password" name="password" id="pwd" />
    <input  required type="submit" name="submit" value="Login" />
</form>

答案 2 :(得分:9)

一个简单的解决方案就是这样的

$( "form" ).on( "submit", function() { 

   var has_empty = false;

   $(this).find( 'input[type!="hidden"]' ).each(function () {

      if ( ! $(this).val() ) { has_empty = true; return false; }
   });

   if ( has_empty ) { return false; }
});

注意:jQuery.on()方法仅适用于jQuery版本1.7+,但它现在是附加事件处理程序的首选方法。

此代码循环遍历表单中的所有输入,并通过返回false来阻止表单提交(如果其中任何一个没有值)。请注意,它没有向用户显示有关表单无法提交的原因的任何类型的消息(我强烈建议添加一个)。

或者,您可以查看jQuery validate插件。它做到了这一点以及更多。

注意:此类技术应始终与服务器端验证结合使用。

答案 3 :(得分:3)

您需要在表单submit事件中添加处理程序。在处理程序中,您需要检查每个文本字段,如果值非空,请选择元素和密码字段。

$('form').submit(function() {
     var res = true;
     // here I am checking for textFields, password fields, and any 
     // drop down you may have in the form
     $("input[type='text'],select,input[type='password']",this).each(function() {
         if($(this).val().trim() == "") {
             res = false; 
         }
     })
     return res; // returning false will prevent the form from submitting.
});

答案 4 :(得分:3)

我真的很讨厌不告诉我输入缺失的表格。 所以我改进了多米尼克的答案 - 谢谢你。

在css文件中设置&#34; borderR&#34;边界的类别为红色。

$('#<form_id>').submit(function () {
    var allIsOk = true;

    // Check if empty of not
    $(this).find( 'input[type!="hidden"]' ).each(function () {
        if ( ! $(this).val() ) { 
            $(this).addClass('borderR').focus();
            allIsOk = false;
        }
    });

    return allIsOk
});

答案 5 :(得分:1)

你应该尝试使用jquery validate plugin:

$('form').validate({
   rules:{
       email:{
          required:true,
          email:true
       }
   },
   messages:{
       email:{
          required:"Email is required",
          email:"Please type a valid email"
        }
   }
})

答案 6 :(得分:1)

function isEmpty(val) {
if(val.length ==0 || val.length ==null){
    return 'emptyForm';
}else{
    return 'not emptyForm';
}

}

$(document).ready(function(){enter code here
    $( "form" ).submit(function( event ) {
        $('input').each(function(){
           var getInputVal = $(this).val();
            if(isEmpty(getInputVal) =='emptyForm'){
                alert(isEmpty(getInputVal));
            }else{
                alert(isEmpty(getInputVal));
            }
        });
        event.preventDefault();
    });
});

答案 7 :(得分:0)

var save_val = $("form").serializeArray();
$(save_val).each(function( index, element ) {
    alert(element.name);
    alert(element.val);
});
相关问题