3

I would like to perform form validation using JavaScript to check for input field only to contain numeric characters.So far, the validation checks for the field not being empty - which works fine.However, numeric characters validation is not working.I would be grateful for any help.Many thanks.

<script type="text/javascript">
//form validation
function validateForm()
{
var x=document.forms["cdp_form"]["univer_number"].value
if (x==null || x=="")
  {
  alert("University number (URN) field must be filled in");
  cdp_form.univer_number.focus();
  return false;
  }
else if (is_valid = /^[0-9]+$/.test(x))
    {
    alert("University number (URN) field must have numeric characters");
    cdp_form.univer_number.focus();
    return false;
  }
}
</script>


<input type ="text" id="univer_number" maxlength="7"    size="25"   name="univer_number" />
Igor Novoselov
  • 71
  • 3
  • 5
  • 10
  • "Not working" is a poor way to describe a problem. What is not working? Are you getting errors? – Oded Apr 26 '11 at 19:48
  • It doesn't provide validation error which I wanted. Don't worry the problem has been solved - see accepted answer. – Igor Novoselov Apr 26 '11 at 20:03
  • I worry, as I hope your next questions have better descriptions. http://tinyurl.com/so-hints – Oded Apr 26 '11 at 20:06

7 Answers7

2

Your test condition is a bit strange:

else if (is_valid = /^[0-9]+$/.test(x))

Why have the redundant comparison to is_valid? Just do:

else if (/^[0-9]+$/.test(x))

Though the regex you are using will match numerals and only numerals - you need to change it to match anything that is not a numeral - like this /^[^0-9]+$/.

Better yet, get rid of the regex altogether and use IsNumeric:

else if (!IsNumeric(x))
Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Rather than using Regex, if it must only be numerals you can simply use IsNumeric in Javascript.

IsNumeric('1') => true; 
IsNumeric('145266') => true;
IsNumeric('abc5423856') => false;
Mikecito
  • 2,053
  • 11
  • 17
2

You need invert your regular expression (add ^ inside [0-9]):

/^[^0-9]+$/
Egor
  • 120
  • 6
1

Your pattern will still accept this input <b>#@$@#123 or ad!@#12<b>. Use this pattern I created:

/[a-zA-Z-!@#$%^&*()_+\=\[\]{};':"\\|,.<>\/?]/ 

This pattern will check if it is alphabetic and special characters.

Papershine
  • 4,995
  • 2
  • 24
  • 48
Don
  • 11
  • 2
1

On your line that says else if (is_valid = /^[0-9]+$/.test(x)), you're doing a simple assignment instead of testing that it is actually matching the regex.

Donnie
  • 6,229
  • 8
  • 35
  • 53
0

I know this is an old post but I thought I'd post what worked for me. I don't require the field to be filled at all but if it is it has to be numerical:

function validateForm()
{
 var x=document.forms["myformName"]["myformField"].value;

if (/[^0-9]+$/.test(x))
    {
    alert("Please enter a numerical amount without a decimal point");
    myformName.myformField.focus();
    return false;
  } 
}
karthikr
  • 97,368
  • 26
  • 197
  • 188
0

You need to test for the negation of the RegExp because you want the validation to alert upon failure, so just add ! in front of it:

else if (is_valid = !/^[0-9]+$/.test(x))

See example →

mVChr
  • 49,587
  • 11
  • 107
  • 104