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
    Regular Coder
    Join Date
    Oct 2011
    Posts
    116
    Thanks
    19
    Thanked 1 Time in 1 Post

    Regular Expressions

    Dear Experts

    I have folloiwng codes
    Code:
    <html>
    <head>
    <body>
    <center>
    
    <script language="JavaScript">
    function checkpostal(){
    var re=/^\D{A-Z}$/ 
    
    if (myform.myinput.value.search(re)==-1)
    { 
    alert("Good")
    }else{
    alert("Bad")
    }
    }
    </script>
    
    <form name="myform">
    <input type="text" name="myinput" size=15>
    <input type="button" onClick="checkpostal()" value="check">
    
    </form>
    </center>
    </body>
    </html>
    With Onclick, I want to test input through regular expression
    If entered values is between a-z or A-Z then
    it must display
    alert("Good")
    else
    alert("Bad")

    Please help
    Last edited by tqmd1; 12-29-2011 at 05:04 AM.

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You're using {} instead of [] for a range of characters. Also presumably you allow more than one letter so use the + quantifier.

    You can check out RegExp documentation here.

    Code:
    <doctype html>
    <head>
    <body>
    
    <script type="text/javascript">
    
    function checkpostal( elem )
    {
      var re = /^[a-z]+$/i; 
    
      alert( elem.value.search( re ) == -1 ? "Bad" : "Good" );
    }
    
    </script>
    
    <form name="myform" style='text-align:center'>
     <input type="text" name="myinput" size=15>
     <input type="button" onClick="checkpostal( this.form.myinput )" value="check">
    </form>
    
    </body>
    </html>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You could improve the code by deleting all characters except letters and writing the result back to the field before making the test. Is there no limit to the number of letters which the user may enter?

    Perhaps you are looking for
    Code:
    var re = /^[a-z]{2,5}$/i; // 2 to 5 letters case insensitive

    You can test your regular expressions at: http://www.claughton.clara.net/regextester.html
    Last edited by Philip M; 12-29-2011 at 08:15 AM.

    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 to the CF scene
    Join Date
    Dec 2011
    Location
    ca ,usa
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx Actually i also face this kind of problem
    After reading this my problem resolve......


  •  

    Posting Permissions

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