jfishbow
jfishbow

Reputation: 253

How can I check if form input is not all number before submitting form and show message?

I have a form

  <form id="login_form" action="login.aspx" method="post" onSubmit="return  isNumeric();">
       <label for="nid"> Number:</label>
       <input type="hidden" name="Context" value="NonAdminLogin" />
       <input type="text" name="nid" id="numbers" maxlength="25" tabindex="1" />
       <input type="submit" class="button" value="Log In" tabindex="3" />
   </form>

and before i submit it I want to make sure that the input name="nid" does not contain ALL NUMBERS.

For example if it has '9909k' it good , but if it has '45645' then I would like to show a message below form.

I have try this fuction

function isNumeric(){

var numexp = /^[0-9]+$/;
if(document.getElementById('numbers').value.match(numexp)){
return true;
}else{
alert("Entry must be a number");
elem.focus();
return false;
}
}

to validate if its a numer of not, but does not work.

http://jsfiddle.net/9x6rz5Lu/

Upvotes: 0

Views: 21

Answers (1)

scartag
scartag

Reputation: 17680

Your fiddle works but you need to change the script from onLoad to No wrap - in <head>.

and you've used an undeclared variable.

elem.focus();

As @imtheman has done in his fiddle below. change elem.focus(); to.

document.getElementById('numbers').focus();

Upvotes: 3

Related Questions