Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numeric Validation 1

Status
Not open for further replies.

DirtyB

Programmer
Mar 13, 2001
159
US
I need a JavaScript function that when called, will check to see make sure that the textbox contains only numbers OR it is ok if the textbox is totally blank. After the validation to make sure it is blank, or numeric, then the form needs to be submitted.

I have found several code snippets for this on the web, but I can't seem to get any of them to work. I've tried running loops on each character on the box, and i've tried isNAN and other things. ANY help would be appreciated.

Thank You
 
function checkForm(x)
{
var reg = /^\d*$/g;
for(var i = 0; i < x.elements.length; i++)
{
if(!reg.test(x.elements))
{
alert('please enter a number in field'+x.elements.name);
x.elements.focus();
return false;
}
}
return true;
}

<form onsubmit=&quot;return checkForm(this);&quot;> luciddream@subdimension.com
 
Here is what I have:

function ValidateForm(x)
{
var reg = /^\d*$/g;
for(var i = 0; i < x.elements.length; i++){
if(!reg.test(x.elements)){
alert('please enter a number in field'+x.elements.name);
x.elements.focus();         
return false;
}
}
return true;
document.Survey.submit();
}




<INPUT type=&quot;button&quot; value=&quot;Submit Data&quot; id=submit1 name=submit1 OnClick=&quot;return ValidateForm(Survey);&quot;>


What am I doing wrong. I do realize that the function will return true or false,and I will accomodate for that in the form tag, but I am getting illegal character messages and messages that say my function is not defined. Survey is the name of the form.

thanks again
 
yeah, i have no idea why that is in Italics
 
you can do with the onclick of the submit button, but, i think its better to use the onsubmit of the form...

but, if you are going to use the submit button's onclick, use:

onclick=&quot;return ValidateForm(document.Survey);&quot; luciddream@subdimension.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top