Trick to check your Regular expression


Thursday, March 10, 2011

Hello Friends,

Today i am going to explain you or provide you a trick by using which you can check your regular expression on page.

Let consider, we want to validate number input,
Like, you have "Mobile Number" field on your page, then it require only number input,

so here you will see, if you will enter Interger in text box, it will allow you to enter otherwise it will return error message.

So lets start here,

click on Start button at left botton of your destop screen, type notepad in run and open one notepad page.

Paste below code and select filetype as "all" and save as html page.

and then click on that page and open HTML page.

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
     <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
         
     <script type="text/javascript">
            function rajesh(){
             alert(validate(document.getElementById("txtemailid").value));
             }
     
             function validate(text){
            return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(text);
             }
        </script>

    </head>
    <body>
    <input type="text" id="txtemailid" name="txtemailid"/>
        <input type="button" value="check" onclick="return rajesh();"/>
    </body>
</html>




so you will see one text box, in which if you enter some text, it will return false, which means you cannot enter text in that field,



so now if you will enter number in that textbox then you will see it will return true, which means you can enter number in that field.




Explaination of code.
Step 1. Add this two files in head part of your page.

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>

Step 2. Add this script in head part of your page.

    <script type="text/javascript">
            function rajesh(){
             alert(validate(document.getElementById("txtemailid").value));
             }
     
             function validate(text){
            return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(text);
             }
        </script>

in function rajesh(), you can see, i am calling another function named validate().

In validate function, you can see, i have kept your regular expression and passing value with name "text".

which will validate value "text" with regular expression and return true or false to you.

Step 3. In body part you can add UI part, which will have one textbox and submit button.

I am done now, so you can check this out at your end or if you have any issue or problem, please comment me, i will send you reply as soon possible.

or you can use this link to check our live code, http://jsfiddle.net/DhBrV/ or you can visit http://www.induway.com. to know more...


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Senior Asp.Net Developer

Email: raj143svmit@gmail.com
e-Procurement Technologies Ltd (India)
www.abcprocure.com

No comments :