• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

Only allow numbers, dashes (-) and comma's (,) in text field

Hi,

I would like to only allow numbers, dashes (-) and comma's (,) to be entered into a text field.

How should i do this? Without the use of jQuery please.

Thanks!
0
peps03
Asked:
peps03
1 Solution
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
try this:

var someName = document.getElementById("sometextField");
if(/^[-,a-z0-9-]+$/i.test(someName.value))
{
    alert('Name can only be alpha numeric with hypen or comma.');
    return;
}
0
 
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/Cnc6V/
function f1(evt)  {
    var e = event || evt;
    var c = e.which || e.keyCode;
    return !( (c<48||c>57) && c!=45 && c!=44 );
}¿

Open in new window

with :
<input type="text" onkeypress="return f1(event)" />¿

Open in new window

0
 
peps03Author Commented:
Thanks!!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now