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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Form Validation

Status
Not open for further replies.

angel03

Programmer
Joined
Nov 17, 2003
Messages
1
Location
US

Q ) Below is my code & I keep getting an error , where am I wrong ? And I am also not getting the age validation part right. I want it to be a number & between 1& 100, how to write that ???

<HTML>
<HEAD>
<TITLE ></TITLE>

<SCRIPT>

// check for valid email address format //

function validEmail(email) {

invalidChars = &quot; /:,;&quot;;

if (email == &quot;&quot;) return false;

for (i = 0; i < invalidChars.length; i++) {
badChar = invalidChars.charAt(i);

if (email.indexOf(badChar, 0) > -1) return false;
}

atPos = email.indexOf(&quot;@&quot;, 1);

if (atPos == -1) return false;

if (email.indexOf(&quot;@&quot;, atPos + 1) != -1) return false;

periodPos = email.indexOf(&quot;.&quot;, atPos);

if (periodPos == -1) return false;

if (email.lastIndexOf(&quot;.&quot;) + 3 > email.length) return false;
if (email.length > email.lastIndexOf(&quot;.&quot;) + 4) return false;

return true;
}

// check for valid Postal code which is either 5 or 9 digit zip with a hyphen after 5 digits.//


function validateZIP(zipfield) {
var valid = &quot;0123456789-&quot;;
var hyphencount = 0;

if (field.length!=5 && field.length!=9 && field.length!=10) {
return false;

}
for (var i=0; i < field.length; i++) {
temp = &quot;&quot; + field.substring(i, i+1);
if (temp == &quot;-&quot;) hyphencount++;
if (valid.indexOf(temp) == &quot;-1&quot;) {
return false;
}
if ((field.length ==9) | | (field.length == 10)){
if ((hyphencount != 1) && (field.charAt(5)!=&quot;-&quot;)) {
return false;
}}
}
return true;
}



// Check if name fields are valid characters//

function alphaOnly(theString) {
var OK = true;
for (var i=0;i<theString.length;i++) {
theChar = theString.charAt(i);
if ( (theChar >= &quot;a&quot;) && (theChar <= &quot;z&quot;) )
continue;
if ( (theChar >= &quot;A&quot;) && (theChar <= &quot;Z&quot;) )
continue;
OK = false;
}
return OK;
}

// check if number fields are numeric//

function numbersOnly(theString) {
var OK = true;
for (var i=0;i<theString.length;i++) {
theChar = theString.charAt(i);
if ((theChar < &quot;0&quot;) || (theChar > &quot;9&quot;)) {
OK = false;
break;
}
}
return OK;
}


//----------------------------------------------------//


function ValidateInput(form) {

var LB = &quot;\n&quot;; // my notation for Line Break
var msghdr = &quot;Please fill out your:&quot; + LB + LB;
var msg = &quot;&quot;;

if (!form.First.value) msg += &quot;First Name&quot; + LB;
else if (!alphaOnly(form.First.value)) msg += &quot;Invalid First Name&quot; + LB;

if (!form.Last.value) msg += &quot;Last Name&quot; + LB;
else if (!alphaOnly(form.Last.value)) msg += &quot;Invalid Last Name&quot; + LB;

if (!form.Address.value.length == 0) msg += &quot;Address&quot; + LB;

if (!form.City.value) msg += &quot;City&quot; + LB;
else if (!alphaOnly(form.City.value)) msg += &quot;Invalid City Name&quot; + LB;

if (!form.State.value) msg += &quot;State&quot; + LB;
else if (!alphaOnly(form.State.value)) msg += &quot;Invalid State Code&quot; + LB;

if (!form.Postal.value) msg += &quot;Postal Code&quot; + LB;
else if (!validateZIP(form.Postal.value)) msg += &quot;Invalid Postal Code&quot; + LB;


if (form.Age.value.length == 0 || parseInt(form.Age.value) == 0) msg += &quot;Age&quot; + LB;
else if (!numbersOnly(form.Age.value)) msg += &quot;Invalid Age&quot; + LB;

if (!form.Income.options[form.Income.selectedIndex].value == &quot;0&quot;) msg += &quot;Income&quot; + LB;


if (form.Sex[0].checked == false && form.Sex[1].checked == false) msg += &quot;Sex&quot; + LB;

if (!form.EMail.value){
msg += &quot;Email Address&quot; + LB;
}
else if (!validEmail(form.EMail.value)) {
msg += &quot;Invalid Email Address&quot; + LB;
}


// Display an alert if any of the input is missing:
if (msg.length > 0){
alert(msghdr + msg);
return false;
}
else return true;
}

</SCRIPT>

</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot;>
 
Hi,
For validating numbers you can use this javascript function..

<script LANGUAGE=&quot;javascript&quot;>
function OnlyNumeric(intAllowDecimal)
{
var KeyAscii = window.event.keyCode;

if (intAllowDecimal==1 && KeyAscii == 46)
{
return;
}
else
{
if ( KeyAscii < 48 || KeyAscii > 57 )
{ window.event.keyCode = 0; }
}
}
</script>



And you call the above in the following way..
<INPUT type=&quot;text&quot; id=text1 name=text1 onkeypress=&quot;OnlyNumeric(1)&quot; maxlength=3>

Hope this is helpful..
 
you should have a look to regular expression

function numbersOnly(szString)
{
var RegEx = new RegExp();
RegEx = /^\d+$/

return (RegEx.test(szString));
}

for the age between 1 & 100

if (numbersOnly(age) && parseInt(age) > 1 && parseInt(age) < 100)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top