简体   繁体   English

如何在Jquery中验证文本框

[英]How to Validate Text box in Jquery

I want to do the validation to four text box using JQuery 我想使用JQuery对四个文本框进行验证

Coding i have done 我已经完成编码

<table align="center" id="tbl-employee">
<tr>
<td>
<label>Insert Employee Details</label>
</td>        
</tr>
<tr>
<td>
<label id="lbl-name">Name</label>
</td>
<td>:</td>
<td>
<input type="text" id="txt-name" />
</td>
</tr>
<tr>
<td>
<label id="lbl-salary">Salary</label>
</td>
<td>:</td>
<td><input type="text" id="txt-salary" /></td>
</tr>
<tr>
<td>
<label id="lbl-deptid">DeptId</label>
</td>
<td>:</td>
<td><select id="ddl-deptid">
<option value="0">-SelectOne-</option>
<option value="1">SoftWare</option>
<option value="2">CA</option>
<option value="3">Professor</option>
 </select>            
 </td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="button" id="btn-insert" value="Insert" /></td>
</tr>
</table>
}

JavaScript code JavaScript代码

  $(function () {
  $("#btn-insert").click(function () {
        $("#tbl-employee").validate({
            rules: {
                Name: { required: true, minlenth: 50 },
                Salary:{required:true,minlength:9},
                DeptId:"required"

            },
            message: {
                Name:{required:"Please Enter Name",minlength:"Your Name must lessthan 50 characters" }
            },
            submit Handler: function (table) {
                table.submit();

            }
        });

    })
})

Will anyone check my program,If i am not giving any text to the input text box ,while submitting it should display the error message. 任何人都可以检查我的程序,如果我没有在输入文本框中输入任何文本,则在提交时应显示错误消息。

.validate() function of jquery validation is applied for form id not for table id. jQuery验证的.validate()函数适用于表单ID,而不适用于表ID。
You are given .validate() to table id 您获得.validate()表ID

The validate plugin requires ID of the form to be validated unlike the id of the table you are using now tbl-employee . validate插件需要验证表单的ID,这与您现在使用的表的ID tbl-employee Wrap your HTML inside a form and the id of this form to validate. 将您的HTML包装在一个表单中,并包装此表单的ID以进行验证。

$("#id_of_your_form").validate({..

Also, you are not having required attribute for the input textbox. 另外,您没有输入文本框的required属性。 Add it to the desired textboxes and the validations will work. 将其添加到所需的文本框中,验证将起作用。

Eg. 例如。 <input type="text" id="txt-name" required/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM