jQuery Validation [Number]

bikashgosai

bikashgosai

Posted on December 23, 2021

jQuery Validation [Number]

Here is simple jQuery validation.

HTML code:

<form id="formId" method="post">
<div class="body">
<input class="form-control text-right" type="number" name="fieldName" min="0" max="1000" />
</div>
<div class="footer">
<button type="button" onclick="clearValidation()" class="btn btn-sm" >Cancel</button>
<button id="btnSubmit" type="button or submit" class="btn btn-sm">Submit</button>
</div>
</form>
Enter fullscreen mode Exit fullscreen mode

Put this code on document ready.

$("#formId").validate({
        rules: {
            fieldName: {
                required: true,
                number: true,
                min: 1
            }
        },
        messages: {
            fieldName: {
//Custom messages
                required: "Required",
                min: "Value must be greater than 0"
            }
        }
    });
Enter fullscreen mode Exit fullscreen mode

Check if fields are valid according to rules defined above:

var form = $("#formId");
    if (form.valid()) {
        //do something...
    }
else
{
return;
}

Enter fullscreen mode Exit fullscreen mode

This is code for removing validation on form.


function clearValidation() {
    var form = $("#formId").validate();
    form.resetForm();
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
bikashgosai
bikashgosai

Posted on December 23, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

jQuery Validation [Number]
javascript jQuery Validation [Number]

December 23, 2021