Showing posts with label Regular expression Validations. Show all posts
Showing posts with label Regular expression Validations. Show all posts

Regular Expression for Alphanumeric using Javascript

You can use Javascripts to validate this as followes.
Define the Javascript Function "isNumberEvt" on Head Section. and check it on keypress even of text box like "onkeypress="return isNumberEvt(event)"".

Javascript :
    function isNumberEvt(evt)
    {

        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode > 31 && charCode <> 57 && charCode <> 90 && charCode <> 122))
        return false;

        return true;

    }
aspx:

call function onkeypress="return isNumberEvt(event)" even of Textbox.
    asp:textbox id="TextBox1" onkeypress="return isNumberEvt(event)" runat="server" width="238px"

To Validate Numbers Using JavaScript

Use the following function to Check the object does ahve a Numeric value or not.

javascript :
    function Validatenumber(obj)
    {
        if (obj.length !=0)
        {
            var text = /^[-0-9]+$/;

            if ((document.getElementById(obj.id).value != "") && (!text.test(document.getElementById(obj.id).value)))
            {
                alert("Please enter numeric values only");
                obj.focus();
                obj.select();
            }
        }
    }

For Validating the Year Value Using Javascript ...

The following code used to validate "Year" using javascript.

Javascript :
    function ValidateYear(obj)
    {
        if (obj.length !=0)
        {
            var text = /^[0-9]+$/;

            if ((document.getElementById(obj.id).value != "") && (!text.test(document.getElementById(obj.id).value)))
            {
                alert("Please Enter Numeric Values Only");
                obj.focus();
                obj.select();
            }

            if (document.getElementById(obj.id).value.length>4)
            {
                alert("Year is not proper. Please check");
                obj.focus();
                obj.select();
            }
        }
    }