Guest User

Guest User

  • Tech Writer
  • 41
  • 16.4k

Jquery validation for unique numeric field

Sep 5 2015 6:13 PM

How do I validate a field to only except numbers, in my code below I want persal_no to only except numeric values and be unique, as persal_no is a unique number given by the government to teachers. How can this be achieved using query?

    $(document).ready(function () {

    $("#employeeDetailsForm").validate({
        rules: {
            "persal_no":{ required: true, maxlength: true},
            title_id: "required",
            emp_first_name: "required",
            emp_surname: "required",
            gender_id: "required",
            "email_addr": { email: true , required:true
        },
            id_no: "required",
            emp_tel_no: "required",
            emp_cell_no: "required",
            UserId: "required"
        },
        messages: {
            "persal_no": { required: "Please enter employee persal number", maxlength:"Maximum length 10 digits" },
            title_id: "Please select a title from the list",
            emp_first_name: "Please enter employee first name",
            emp_surname: "Please enter employee last name",
            gender_id: "Please select employee gender type",
            "email_addr": { required: "Please enter email address" , email: "Format incorrect" },
            id_no: "Please enter employee ID number",
            emp_tel_no: "Please enter employee telephone number",
            emp_cell_no: "Please enter employee cellphone number",
            UserId: "Please select a User ID"
        }
    });

    $("#create").click(function () {

        if (!$("#employeeDetailsForm").valid()) {
            return false;
        }

        $("#dialog").dialog({
            resizable: false,
            width: 540,
            height: 150,
            modal: true,
            hide: "clip",
            show: "slide",
            buttons: {
                "Confirm": function () {
                    $.ajax({
                        type: 'POST',
                        url: "/EmployeeDetails/Insert",
                        dataType: "json",
                        data: {
                            persal_no: $("#persal_no").val(),
                            title_id: $("#title_id").val(),
                            emp_first_name: $("#emp_first_name").val(),
                            emp_middle_name: $("#emp_middle_name").val(),
                            emp_surname: $("#emp_surname").val(),
                            gender_id: $("#gender_id").val(),
                            email_addr: $("#email_addr").val(),
                            id_no: $("#id_no").val(),
                            emp_tel_no: $("#emp_tel_no").val(),
                            emp_cell_no: $("#emp_cell_no").val(),
                            UserId: $("#UserId").val(),
                            __RequestVerificationToken: $('input[name=__RequestVerificationToken]').val()
                        },
                        success: function (data) {
                            //var value = data.message;
                            //alert(value);
                            $("#dialog").dialog("close");
                            $("#dialogSuccess").dialog({
                                resizable: false,
                                width: 540,
                                height: 150,
                                modal: true,
                                show: "slide",
                                buttons: {
                                    "OK": function () {
                                        $("#dialogSuccess").dialog("close");
                                        window.location = "/EmployeeDetails/Search";
                                    }
                                }
                            });
                        },
                        error: function (xhr) {
                            alert(xhr.responseText);
                            $("#dialog").dialog("close");
                        }
                    });
                    //document.categoryForm.submit();
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
    });
});

 


Answers (1)