SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to check field for invalid characters

    Just wondering if anyone can help me with this since I am having some trouble... any sample code would be extremely useful.

    I'm trying to validate a username field so they cannot enter invalid characters... ie. they can only enter letters and numbers.

    Can anyone provide some examples of sites that do this? or can someone please provide an example here?

    I'm tearing my hair out right now and I'm getting all stressed over this

  2. #2
    SitePoint Wizard Goof's Avatar
    Join Date
    Feb 2001
    Location
    Pittsburgh, PA
    Posts
    1,154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use regular expressions:
    Code:
    function isValidUsername(myStr) {
      var pattern = /^[a-z0-9]+$/i;
      var StringMatches = myStr.match(pattern);
      if (StringMatches) {
        return true;
      }
      else {
        return false;
      }
    }
    This function will return true if the username only contains letters and numbers, and false if it contains anything else, so you could use it like this:

    Code:
    <SCRIPT LANGUAGE="JavaScript1.2">
    <!--
    function isValidUsername(myStr) {
      var pattern = /^[a-z0-9_\s]+$/i;
      var StringMatches = myStr.match(pattern);
      if (StringMatches) {
        return true;
      }
      else {
        return false;
      }
    }
    
    function verifyForm() {
      if (isValidUsername(document.myForm.username.value)) {
        document.myForm.submit()
      }
      else {
        alert('Invalid username.  Usernames can only contain alphanumeric characters.');
      }
    }
    //-->
    </SCRIPT>
    <form name="myForm" action="something.shtml" onSubmit="verifyForm(); return false;">
    <input type="text" name="username"><input type="submit">
    </form>
    Hope that helps,
    Goof
    Nathan Rutman
    A slightly offbeat creative.

  3. #3
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic!! I'm going off to try this code. Hopefully it works (fingers crossed)...

    Thanks for your help Goof!!


  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good response Goof.

    However i'm not sure that matching the ^ $ will help, since
    a text area would return this as the first line if a return were present - and ignore the rest ... allowing for other characters.

    Your regex doesn't allow for sensilble characters in a
    username ( quite rightly since dragon hawk didn't ask for them )
    But wouldn't it be better to allow things like _- ?

    What i normally do is convert to hex and then disregard the
    response if the characters are above a set number.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh well...

    I couldn't get the js to work with the functions that I'm using with PHP, but it works in a standalone html file.

    So after wasting 3 hours on it, I fixed it up using PHP in an hour... And all that time searching was supposed to save me that one hour in programming

    Hmmm, well, that taught me a lesson... Stick with what you know unless you have time to learn what you don't


  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol @ last expression!

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •