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

    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Find exact string length and remove


    Hi, how do I validate a string and remove it if it doesn't match the desired length? For example if a phone number is too short or too long, remove it, but if it is the desired length of 11 characters, leave as is?

    Thanks in advance

    Andy
  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
    You don't need a regular expression for that. Your programming language will have some way of finding the length of a string (e.g. Java's String class has a length() method, Perl has a 'length' function, etc.).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0
    You're right Ishnid, I can use my language to check the length, however, I have a group of (pre-built) regexs to remove non-alpha characters, remove spaces etc and I'll like to add to the functionality and make it invisible to my users.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,905
    Rep Power
    6351
    After you remove the invalid characters, check the length.

    Be warned though that if someone types "123 14th street, new york new york 10036" and you strip out the non-numeric characters, the result will APPEAR to be a phone number, but won't be.

    -Dan

    Comments on this post

    • jalucas agrees : Clever
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo