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

    Regexp not working ?

    I have now a regexp

    Code:
    var stringExp = new RegExp(/\W/);
    
    if (!stringExp.test(document.getElementById(inputboxid).value)) {
    
    }
    else {
    
    }
    This is working and I can fill in a till z / A till Z and _
    However I want to have a till z / A till Z and _ - and space.

    I got this regexp

    Code:
    var stringExp = new RegExp(/^[a-zA-Z]+l{2}[a-zA-Z\s]*$/);
    But this aint working ...

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Not sure exactly what you want:-

    var stringExp = new RegExp(/[^\w\s\-]/); // match anything NOT a-z A-Z 0-9 underscore space hyphen


    If you do not want numerals

    var stringExp = new RegExp(/[^a-z\s\-\_]/i); // match anything NOT a-z A-Z space hyphen underscore

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Euhm what I don't want is that users can fill in

    &^%$ (special characters) etc
    Only numbers, space, _ - , letters (a-z / A-Z) and .

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Mick.Dundee View Post
    Euhm what I don't want is that users can fill in

    &^%$ (special characters) etc
    Only numbers, space, _ - , letters (a-z / A-Z) and .
    I don't see any mention of . (dot) in your first post.

    var stringExp = new RegExp(/[^\w\s\-\.]/); // match anything NOT a-z A-Z 0-9 underscore space hyphen period

    i. e. returns true if any other character is present in the string, in which case the field is invalid and should be re-entered by the user.

    \w matches any alpha-numeric character including the underscore. It is equivalent to [a-zA-Z0-9_]
    ^ within the square brackets means "not".

    if (stringExp.test(document.getElementById(inputboxid).value)) { // fail the test if true

    You may think it a good idea to strip leading spaces (prevent all spaces entered in the field):-

    x = document.getElementById(inputboxid).value;
    x = x.replace(/^\s+/,""); // strip leading spaces
    if (stringExp.test(x)) { // fail the test if true

    Always go to other people’s funerals, otherwise they won’t come to yours.
    Last edited by Philip M; 06-16-2008 at 05:04 PM. Reason: Note re leading spaces

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mhhh tried solutions but can't get it to work.

    To clarify my start post:

    I want to check the username on valid input.

    Valid usernames are

    Firstname.Lastname
    Firstname Lastname
    Firstname_Lastname
    Firstname-Lastname
    Firstname
    Lastname

    And the username may be letters (a till z) or numbers (0-9)

    Illegal characters (on every position) are for example

    !@#$%^&*()+,?<>

    So some examples of illegal usernames

    First@Name Lastname
    FirstName' Lastname
    %
    #Lastname
    '
    Firstname in '

    etc
    Last edited by Mick.Dundee; 06-17-2008 at 10:17 AM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Well, it works fine for me.

    You can test the regex thus:-

    Code:
    <input type = "text" id = "inputboxid" size = "20" onblur = "chk()">
    
    <script type = "text/javascript">
    function chk() {
    inp = document.getElementById("inputboxid").value
    if (/[^\w\s\-\.]/.test(inp)) {  // i.e. if an invalid character is present in the string
    alert ("Invalid")
    }
    else {
    alert ("OK")
    }
    }
    </script>

    What about people who have apostophes in their names (O'Reilly etc.)?


    You can test your regular expressions at: http://www.claughton.clara.net/regextester.html
    Last edited by Philip M; 06-17-2008 at 11:52 AM.

  • #7
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Very strange, I tried again (first in a clean .html file) and it works.
    Then I changed again my .JS file and it works :S

    Maybe cached JS file ?

    Good question, will have think about it because I won't want those names showing as invalid


  •  

    Posting Permissions

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