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" />
3
  • "Not working" is a poor way to describe a problem. What is not working? Are you getting errors? Commented Apr 26, 2011 at 19:48
  • It doesn't provide validation error which I wanted. Don't worry the problem has been solved - see accepted answer. Commented Apr 26, 2011 at 20:03
  • I worry, as I hope your next questions have better descriptions. tinyurl.com/so-hints Commented Apr 26, 2011 at 20:06

7 Answers 7

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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

/^[^0-9]+$/

Comments

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))

Comments

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.

1 Comment

As strange as it seems, it still yields the correct result (at least fiddling in chrome)
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.

Comments

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 →

1 Comment

Many thanks for your answer.It works - very easy to overlook such a mistake.
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;
  } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.