SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegExp (all alphanumeric with length between x and y)

    Hi guys, new with regexp (and this forum! hi all!).

    hope you can shed some light for me on some areas of regexp im not sure about.

    ill start over with my problem, basicaly im doing two stages of validation on a field, and it needs to validate ranges of string types.

    so basicaly i found regexp, and i worked a string up that would select all alphanumeric characters, and then use if statements to check if the length was between 2 values. cant quite remember, think it was

    /[a-zA-Z0-9]/g

    i cant be sure, im still at the stage of having to look over my regexp, cant remember off hand how to do things perfectly.

    i wanted to see if i could expand upon this and use test to return a true or false to see if the string matched 'validation'

    i came up with;

    re = new RegExp('[\\w+]{5,50}','g');

    what i was hoping was it would rather all the characters that are alphanumeric (\w i believe? eg a-zA-Z0-9? correct me if wrong!) and then return true or false if it was between the range of 5-50 characters in length.

    at first i thought it worked, but i found when i used say @ or anything else non-alphanumeric, to split words into 2 or 3 words with a length of less than 5, it would not validate, which made me realise its validating the length on each match of chars it finds, and not the entire collection of alphanumeric characters i want.

    for example the string.

    "regexp is great"

    would be fine.

    but

    "reg//exp is gr//eat"

    would not validate, im guessing this is because its actually validating this range {5,50} on each match it makes, rather than the whole.

    really what i want it to do is

    "reg//exp is gr//eat"

    filter this to a alphanumeric string;

    "regexp is great" (maybe "regexpisgreat"?)

    then check if the length of that is between {5,50}.

    im guessing i should be nesting a regexp inside another regexp or something?

    really want to avoid use of if statements to check, would rather a true false reply.

    thanks in advance!

    Steve

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, let's strip all non-alphnum chars from the string

    Code:
    str = "reg//exp is gr//eat";
    cleanStr = str.replace(/\W/g, "")
    Lets get the length of "clean" string and test if it's between 5 and 50

    Code:
    len = cleanStr.length;
    alert(len >= 5 && len <= 50)
    And now let's make our code nicer. To be able to test gracefully we extend javascript language by defining the "between" method :

    Code:
    Number.prototype.between = function(min, max) {
    	return this >= min && this <= max;
    }
    
    // some tests
    
    alert((100).between(10, 110)) // true
    alert((200).between(10, 110)) // false
    And finally, let's put it all together:

    Code:
    // final code
    
    str = "reg//exp is gr//eat";
    ok  = str.replace(/\W/g, "").length.between(5, 50);
    
    if(ok)
    	alert("String is correct")
    else
    	alert("String is incorrect")
    hth.

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks stereofrog,

    that looks like a good way to approach it, guess its not realisticaly possible as complete regexp, but your idea is still cleaner than my original concept, thanks alot .


Bookmarks

Posting Permissions

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