meena
meena

Reputation: 245

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.

Upvotes: 0

Views: 385

Answers (2)

Pramod Karandikar
Pramod Karandikar

Reputation: 5329

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/>

Upvotes: 0

jay.jivani
jay.jivani

Reputation: 1574

.validate() function of jquery validation is applied for form id not for table id.
You are given .validate() to table id

Upvotes: 1

Related Questions