#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Smile A few regex questions, please help


    Hi there,

    I just learned regex, i tried to use javascript .test function to validate the following pattern but failed, i was told they are kind of java regex, but i do not know what is difference between java regex and javascript regex, please help:

    1. [\p{Alpha}\.]{0,6} to validate GENDER ,it means that 0 to 6 number is allowed, right?
    2. [\p{Alpha}\._ -]{0,31} to validate STATE ,0 to 31 number will be fine, right?
    3. [\p{Alnum}\. -]* to validate ZIP CODE, nubmer and alpha will be fine, right?
    4. \+?[\d\.() -]{0,28}[\p{Alpha}\.\+ ]{0,6}?\d{0,4}? to validate PHONE NUMBER.


    I used the following JAVASCRIPT code,

    function validation(pattern) // pattern is from above
    {
    var regex_pattern = new RegExp(pattern);
    if (!regex_pattern.test(value))
    {
    alert("invalid data");
    }
    }

    when i input #%×%% as value,all the above did not show alert, which suppose only allow number and alphabet, could anyone help to figure out what is wrong with it, thanks a lot for help!!
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    JavaScript is not one of my strongest points, but I don't believe its regexp engine supports \p{Alpha} and the like.

    I'm not convinced regexps are the way to go for some of these either. For instance, gender can presumably be 'male' or 'female', in which case there's no point in allowing any word up to six characters to be input. Why not just test to ensure your variable contains one of the two legal options.

    The same thing applies to "STATE" (assuming US): there are a finite number of states that can be checked easily enough. Indeed, most sites use the 2-letter abbreviation rather than the full state name, which helps a lot.

    One last point: within a character class, the 'dot' character has no special meaning and so there's no need to escape it with a backslash.

    Comments on this post

    • prometheuzz agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0
    Thank you ishnid very much for your information! Appreciate it!

    Regards,

IMN logo majestic logo threadwatch logo seochat tools logo