Navigation

Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Thursday, 21 November 2013

Number type input fields validation using JQuery

Using JQuery it is easy to validate numeric data in input fields.

Below code validates numeric input in the input type fields.
For this we only need to add .number as class for the input html tags in which we need to validate numeric data.

$('input.number').blur(function () {

    var value = $(this).val();

    if (value == undefined || value == "") {
        $(this).parent().find("span").remove();
        return;
    }
    if (isNaN(value)) {
        $(this).parent().find("span").remove();
        $(this).parent().find("br").remove();
        $(this).parent().append("</br><span class='error'>Please Enter Number</span>");
    }
});

Above blur event shows the Please Enter Number error message just after user moves cursor from input field.