SitePoint Sponsor

User Tag List

Page 1 of 5 12345 LastLast
Results 1 to 25 of 120
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    username verification

    Hi all,
    I have written a function for accepting name and should be atleast minimum 4 characters.
    It is accepting all characters including special characters.
    Now i want my function to accept only a-z0-9 and _(underscore),-(hyphen)
    and .(dot)
    Below is my function

    function checkName(form) /* for real name verification */
    {
    if (form.realname.value == '')
    {
    alert('Error: Username cannot be blank!');
    form.realname.focus();
    return false;
    }
    else if(form.realname.value.length < 4)
    {
    alert("UserName should be atleast 4 characters long");
    return false;
    }

    return true;
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    Now i want my function to accept only a-z0-9 and _(underscore),-(hyphen)
    Do you think that a regular expression would be useful to achieve that?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first Congratulations for being javascript of the year 2010.

    any thing as long as the function is correct to accept the special characters as .(dot),-(hypen),_(underscore)

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    any thing as long as the function is correct to accept the special characters as .(dot),-(hypen),_(underscore)
    Here's something that should help you then. It's a regular expression that matches anything that is not alphabetic, or a dash or an underscore.

    Code javascript:
    /[^a-z_-]/i

    The [url="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec"[/url] documentation page shows you how to use it.

    If it matches anything, those things that it matches are characters that are not allowed.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i need to accept the above three special characters in username for my function
    to fit

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    i need to accept the above three special characters in username for my function
    to fit
    Then you can add the full stop to the list of characters.

    Code javascript:
    /[^a-z_.-]/i
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then numbers also i can add right 0-9 as
    /[^a-z0-9_.-]/i

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    then numbers also i can add right 0-9 as
    /[^a-z0-9_.-]/i
    Correct.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whether is this also correct

    var oRE = /^[\w\d-.]{4,}$/g;
    var isCorrectFormat = oRE.test(text);
    if (!isCorrectFormat)
    {
    alert("Invalid characters in username. It can only contain...");
    return false;
    }

    /[^a-z0-9_.-]/i

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    whether is this also correct
    Yes, that is also another way to do it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank u

  12. #12
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi dude the function is not working
    it is accepting other functions such as !,@,# and so on other than .,_,-
    below is the code i have written..

    Code:
    function checkName(form)          /* for real name verification */
       {
         var oRE =/[^a-z0-9_.-]/i;
         var isCorrectFormat = oRE.test(form);
          if (!isCorrectFormat)
           {
             alert("Invalid characters in username. It can only contain 0-91-9._-");
             return false;
            }
    
      else  if (form.realname.value == '')
           {
               alert('Error: Username cannot be blank!');
                form.realname.focus();
               return false;
           }
                   else if(form.realname.value.length < 4)
                   {
                   alert("UserName should be atleast 4 characters long");
                   return false;
                   }
    
    
           return true;
    }
    Last edited by paul_wilkins; Jul 19, 2011 at 04:13. Reason: add code tags

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    hi dude the function is not working
    The code you got from the other chap checks for valid characters. The regular expression I was helping you with checks for invalid characters.

    Do not mix.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    means the code you given will work for other than a-z0-9and .,_,-
    is that so

  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    means the code you given will work for other than a-z0-9and .,_,-
    is that so
    pardon?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #16
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    means the regular expression u given will work other than that 0-9a-z and .(dot), -(hypen),_(underscore) ?
    my problem is it should like something like this
    username is ravi_951 or ravi.951 or ravi-951
    u find in gmail right accepting _ and .

  17. #17
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    means the regular expression u given will work other than that 0-9a-z and .(dot), -(hypen),_(underscore) ?
    No, that's not the right. His regular expression checks that all the characters are valid. The regular expression that I was helping you with checks if any of the characters are not valid.

    Do you understand the difference?

    His was checking that only cats, dogs, and chickens were present.
    Mine was checking if any of them were not cats, dogs, or chickens.

    Both achieve the same end goal, just from different directions.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #18
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then if my username is ravi@951 it should not work right
    but the above function is working for that.

  19. #19
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    then if my username is ravi@951 it should not work right
    but the above function is working for that.
    If one is checking for a positive match, and another is checking for a negative match, you cannot just swap them around like you did. There is more to crafting it together than you currently understand, or sadly, that I at nearly midnight have the time to convey.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  20. #20
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k then give the regular expression to check that username should only contain
    only 3 special characters as .(dot),_(underscore) and-(hypen).
    as such ravi.951 and ravi_951 and ravi-951 should work.

  21. #21
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    k then give the regular expression to check that username should only contain
    only 3 special characters as .(dot),_(underscore) and-(hypen).
    as such ravi.951 and ravi_951 and ravi-951 should work.
    Well with what you have there, it could be:

    Code javascript:
    /^[a-z]{4}[._-]\d{3}/i
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  22. #22
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so numbers also
    /^[a-z][0-9]{4}[._-]\d{3}/i
    right

  23. #23
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    so numbers also
    /^[a-z][0-9]{4}[._-]\d{3}/i
    right
    Right.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  24. #24
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now for every character it is alerting
    Invalid characters in username. It can only contain...
    if we give ravi also it is not accepting.
    check it once please...

    function checkName(form) /* for real name verification */
    {
    var re =/^[a-z][0-9]{4}[._-]\d{3}/i;
    var isCorrectFormat = re.test(form);
    if (!isCorrectFormat)
    {
    alert("Invalid characters in username. It can only contain...");
    return false;
    }

    else if (form.realname.value == '')
    {
    alert('Error: Username cannot be blank!');
    form.realname.focus();
    return false;
    }
    else if(form.realname.value.length < 4)
    {
    alert("UserName should be atleast 4 characters long");
    return false;
    }


    return true;
    }

  25. #25
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The characters and digits need to be within the same grouping class.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •