Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    77
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question How to use varioable in reguler expression???

    I have called a function with passing 2 arguments in it.i is for id and other is for number.

    I want to use that number in reguler expression.

    Please find below code:

    <html>
    <head>
    <script type="text/javascript">
    function checkNumber(id,num){
    var elementTxt=document.getElementById(id);
    var a=parseInt(num);
    var re5digit='/^\d{'+num+'}$/'; //regular expression defining a 5 digit number


    if(elementTxt.value!=null || elementTxt.value!=""){
    if (elementTxt.value.search(re5digit)==-1 ) //if match failed
    {
    alert("Please enter a valid 5 digit number inside form");
    //return false;
    }
    }
    }
    </script>
    </head>
    <body>
    <input type="text" id='txt'/>
    <input type="button" value="ok" onclick="checkNumber('txt',5)"/>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Your variable re5digit is of type "string" and not of type "RegExp".
    Code:
    var re5digit = new RegExp('/^\d{'+num+'}$/');

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,922
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Regular expressions can be handy for a lot of things, but for simpler tasks they are not the most efficient way. I would suggest determining if the field is not a number and is of a certain length:-


    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    
    function checkNumber(id,num) {
    // num is a number already so no need for parseInt(), which in any case ought to specify the radix
    var elementTxt = Number(document.getElementById(id).value);
    if ((isNaN(elementTxt))  || (elementTxt.toString().length != num)) {
    alert ("Please enter a valid " + num + "-digit number inside form");
    document.getElementById(id).value = "";  // clear the field
    return false;
    }
    }
    
    </script>
    
    </head>
    <body>
    <input type="text" id='txt' size = "5" maxlength = "5"/>
    <input type="button" value="Check It" onclick="checkNumber('txt',5)"/>
    
    </body>
    </html>
    The value of a form field can never be null. Only "" (blank).

    You ought to know that when posting here you are asked to help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.





    Protestors Tried To Spoil Play But Actors Succeeded - Headline in Surrey Adveriser
    Last edited by Philip M; 10-04-2012 at 02:18 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Feb 2007
    Posts
    77
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Thumbs up

    Hey thanks all.
    It worked


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •