SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Test a Field for Valid Characters using REGEX

    Hi.

    I'm writing a form validation function and would like to verify that a field only contains alphanumeric characters.

    How should I structure that statement?

    The following seems logical but doesn't work;


    Code JavaScript:
    			if(document.getElementById('RegisForm_Username').value == /[^a-zA-Z 0-9]/g)
    				{
    				alert("I'm sorry, your username must be alphanumeric (consisting of the letters A-Z or numbers 0-9) and cannot have any special characters.");
    				return false;
    				}

    Any ideas?

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,085
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Use the match function (part of the String prototype):

    Code javascript:
    if(document.getElementById('RegisForm_Username').value.match(/[^a-zA-Z 0-9]/g))

    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    ah, That did it.

    Thanks!

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse


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
  •