SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question accept number and dash only

    hi all,

    i have a javascript code like this:

    return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
    this little code validates if an input is number only. but i want to accept numbers and dash (-) characters in the textbox.

    can anyone try to manipulate this peace of code so that it can also accept dash (-) character.

    thanks in advance!
    happy is the man that finds wisdom....wisdom in {PHP}.


  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    objVal = document.getElementById(objId).value;
    if(!isNaN(objVal) && objVal.match(/\-/)) //do something
    this is just for matching one hyphen
    if you need a format like 99-9999-999-9999
    so use match(/^.*?\-.*?\-.*?\-.*?/)
    cheers

  3. #3
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks sir reminder! works fine now!
    happy is the man that finds wisdom....wisdom in {PHP}.


  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    return Validation.get('IsEmpty').test(v) || /^[-\d]+/.test(v);

    Should do the trick.

  5. #5
    SitePoint Evangelist dscriptor's Avatar
    Join Date
    Oct 2005
    Location
    in front of my computer
    Posts
    571
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks sir scimon! that helped a lot also. thanks!
    happy is the man that finds wisdom....wisdom in {PHP}.



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
  •