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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post REGEXP to verify first chars

    Hi Guys, thanks for spending the time to read my post.

    Outline of what im trying to do:

    a user enters a string of six chars/numbers into a text box...
    The script checks if the first three of those six are equal to a certain value...
    If they are NOT the same it throws up a alert() box..
    If they are the same the script throws up a different alert() box..

    You help is much appreciated..

    Best Regards..

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function isValid(string,cString) {
    if(string.substring(0,3)==cString) alert("Valid!");
    else alert("Invalid!");
    }
    Hope that helps!

    Happy coding!
    Last edited by nolachrymose; 08-01-2002 at 02:25 PM.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: REGEXP to verify first chars

    Originally posted by gate
    The script checks if the first three of those six are equal to a certain value...
    Apart from the solution noloachrymoose posted, I'll think you have to define more precisely what you mean by "certain value". Since you asked about a RegExp validation routine, I'll suppose you'll expect a certain pattern that must be true for the first three characters, such as "valid are only numerical digits and lowercase letters of the english alphabet".

    If that's the case, perhaps this code snippet might help you:

    Code:
    // str is the content of your text field 
    if (!str.match(/^[0-9a-z]{3}/)) {
        alert("invalid");
    }
    You can also enhance the above routine so that you see exactly what was matched and work upon that further in your code.

    Hope that helps.


  •  

    Posting Permissions

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