简体   繁体   中英

How to Validate Text box in Jquery

I want to do the validation to four text box using 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

  $(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.
You are given .validate() to table id

The validate plugin requires ID of the form to be validated unlike the id of the table you are using now tbl-employee . Wrap your HTML inside a form and the id of this form to validate.

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

Also, you are not having required attribute for the input textbox. Add it to the desired textboxes and the validations will work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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