SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Javascript to restrict form input

    Hi:

    I need to use JavaScript to make sure that the input to a particular form field contains nothing but alpha characters, numbers, an underscore (_), a hyphen (-) or a period (.).

    I know how to use indexOf to search for a particular character in a substring, but I don't want to use that for every possible special character out there.

    Similarly, I don't want to have to loop through every character of the input and test it separately.

    Is there a way to accomplish this? Regular expressions? I've looked for the docs for that and can't figure out if it's possible that way. Thanks!

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could always MASK the form input. Try readin Vinny's article on maksing

    http://members.aol.com/grassblad/html/maskFone2.html
    http://members.aol.com/grassblad/html/maskfone.html

    Those are two useful ideas for what you want. And even better they do it real time and not after submission
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regular expressions are definitely the way to go, but remember to implement server-side form validation too - client-side validation is easily circumvented and/or disabled.

    Good links here:
    http://www.google.com/search?q=regul...orm+validation



    M@rco

  4. #4
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can learn how to do this yourself in my Expert JS tutorial on my website, html4me.com.

    http://www.html4me.com

    Good luck-


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
  •