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!


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 :slight_smile:

thanks sir reminder! works fine now! :smiley:

return Validation.get(‘IsEmpty’).test(v) || /[1]+/.test(v);

Should do the trick.


  1. -\d ↩︎

thanks sir scimon! that helped a lot also. thanks!