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

    Regular Expression character match exception

    I've used generic regular expressions (regexp) before but have rarely needed to customize one. What I need to do should be simple:
    Validate a form's input text allowing any character alpha (any case), numeric and special chars) except an asterisk and a percent.

    Using the input text value of "Inc." (should test ok)
    Based on reviewing a number of examples it seems all I needed was:

    ^[^%*]$ ...doesn't seem to test correctly
    [^%*] ...also is not correct

    What is it I'm missing ?
    Any assistance is appreciated.
    Thanks

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    try:

    Code:
    var rexp = new RegExp(/\*|\%/g);
    
    var string = element.value; //the value of your input element
    
    if(rexp.test(string))
    {
    //die
    alert('INPUT NOT VALID!');
    return false; // - add this line if you're inside a function
    }
    I'm not that well versed in regexp - I -think- a single pipe (|) character means 'or'.
    of course, if I'm wrong, you can always use two expressions to test on..

    thus:

    Code:
     
    
    var rexp1 = new RegExp(/\*/g);
    var rexp2 = new RegExp(/\%/g);
    
    var string = element.value; //the value of your input element
    
    if(rexp1.test(string)||regexp2.test(string))
    {
    //die
    alert('INPUT NOT VALID!');
    return false; // - add this line if you're inside a function
    }
    Hope this helps!
    Last edited by Mikebert4; 07-31-2008 at 06:27 PM.

  • #3
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the quick reply...

    Is it possible to do this in one single expression?...
    besides possibly using it in javascript I will have need to include the regexp in a java struts validator.

  • #4
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    I'm sure it is - I wrote myself a Javascript RegExp reference for this very reson... unfortunately it's at work (and hence no use to me now).

    Give me 5 mins, I'll get you a regexp

  • #5
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    Here we go...

    Code:
    /[*%]/g
    Depressingly simple really, if your string contains either of those this expression will return TRUE.

    so in context:

    Code:
    var rexp = new RegExp(/[%*]/g);
    
    var string = element.value; //the value of your input element
    
    if(rexp.test(string))
    {
    //die
    alert('INPUT NOT VALID!');
    return false; // - add this line if you're inside a function
    }
    ok?

  • #6
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Once again, thanks for the quick reply and going out of your way to help...
    I have yet to test it but it looks good...
    I believe using [^%*] should indicate all characters except but I'm going by what I covered earlier...

  • #7
    New Coder
    Join Date
    Jul 2008
    Location
    Peterborough - UK
    Posts
    63
    Thanks
    4
    Thanked 9 Times in 9 Posts
    thats right - though it's easier to test a string for the characters you -don't- want, as opposed to checking them all for what you -do- want...

    Of course, I don't know your context, so that could well work better for you. - don't forget to make it global within the tested string:

    Code:
    /[^%*]/g
    let me know how you fare

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,990
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by Mikebert4 View Post
    Code:
    /[^%*]/g
    /[^%*]/g does not check that the string does not contain either of the characters % or *.
    It will return true if any character is found in the string other than % or *
    so: if string == "abc%*xyz" the regex returns true.
    if string == "abdxyz" the regex again returns true.
    and if string == "%%%%****" the regex returns false (only % and/or * and nothing but occurs in the string).

    The OP requires:-
    "Validate a form's input text allowing any character alpha (any case), numeric and special chars) except an asterisk and a percent".

    The correct regex is
    if (/\%|\*/.test(string.value))
    or if you prefer:- if (/[%*]/.test(string.value))

    which returns true if a % or a * character is found anywhere in the string.
    Note the /g switch is not necessary as a single instance of a % or a * will cause the regex to return true.

    You can reverse the logic thus:-

    if (!/\%|\*/.test(string.value))
    which will return true if no instance of % or * is found in the string. Again, a single instance of % or * will cause the regex to return false.

    See the subtle difference:-

    if (!/\%|\*/.test(string.value)) // returns true if string is abc but false if string is abc%
    if (/[^%*]/.test(string.value)) // returns true if string is abc and true if string is abc%


    You can test your regular expressions at: http://www.claughton.clara.net/regextester.html
    Last edited by Philip M; 07-31-2008 at 09:18 PM.


  •  

    Posting Permissions

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