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

validation not executed 1

Status
Not open for further replies.

mkisanko

Programmer
Jun 16, 2003
5
TZ
Hi I have this code but no validation is done please help I am a bigginer in javascript.
<html>
<head>
<script language=&quot;javascript&quot;>
function display()
{
var pass = string(document.registration.password.value);
var confirmpass = sring(document.registration.confirmpassword.value);

if (pass != confirmpass)
{
alert(&quot;Password and Confirm Password must be the same&quot;);
return false;
}

If (isNaN(document.registration.phonenumber.value)==true)
{
alert(&quot;Phone number must be numbers&quot;);
return false;
}

If (isNaN(document.registration.age.value)==true)
{
alert(&quot;Age must be in number&quot;);
document.registration.age.focus();
return false;
}

If (isNaN(document.registration.zip.value)==true)
{
alert(&quot;Zip must be number&quot;);
return false;
}
}
</script>
</head>
<body>
<form method = &quot;POST&quot; name=&quot;registration&quot; action = &quot; onSubmit = &quot;display()&quot;>
Username                  <input type =&quot;text&quot; name=&quot;username&quot; size=&quot;30&quot;><br>
Password                  <input type =&quot;text&quot; name=&quot;password&quot; size=&quot;30&quot;><br>
Confirm Password     <input type =&quot;text&quot; name=&quot;confirmpassword&quot; size=&quot;30&quot;><br>
Age                           <input type =&quot;text&quot; name=&quot;age&quot; size=&quot;2&quot;><br>
Sex                           <select name=&quot;sex&quot; size=&quot;1&quot;><br>
<option>Female
<option>Male
</select><br>
Address                    <input type =&quot;text&quot; name=&quot;address&quot; size=&quot;30&quot;><br>
Phone Number          <input type =&quot;text&quot; name=&quot;phonenumber&quot; size=&quot;30&quot;><br>
Country                     <select name=&quot;country&quot; size=&quot;1&quot;><br>
<option>Tanzania
<option>Kenya
<option>Uganda
<option>Zambia
<option>Zimbabwe
<option>Mozambique
</select><br>
Zip Code                   <input type =&quot;text&quot; name=&quot;zip&quot; size=&quot;30&quot;><br><br>
            
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
            
<input type=&quot;reset&quot; value=&quot;Reset&quot;>
</form>
</body>
</html>
 
onSubmit = &quot;[red]return[/red] display()&quot;

should do it for you.
Check out my faq faq216-2809

Let us know what happens then.....

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
I agree with HellTel however to make your function work you have to also return &quot;true&quot; if the form is valid otherwise the form will never be submitted - you could change your function to:

function display()
{
var pass = string(document.registration.password.value);
var confirmpass = sring(document.registration.confirmpassword.value);

if (pass != confirmpass)
{
alert(&quot;Password and Confirm Password must be the same&quot;);
return false;
}

If (isNaN(document.registration.phonenumber.value)==true)
{
alert(&quot;Phone number must be numbers&quot;);
return false;
}

If (isNaN(document.registration.age.value)==true)
{
alert(&quot;Age must be in number&quot;);
document.registration.age.focus();
return false;
}

If (isNaN(document.registration.zip.value)==true)
{
alert(&quot;Zip must be number&quot;);
return false;
}
// else form is valid
return true;
}
 
That's right, sorry.
Must learn to read sometime... [bigsmile]

(or ask my boss for more free-time [lol])

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
I made the suggested changes but still the validation is not done and the form is submitted.
<html>
<head>
<script language=&quot;javascript&quot;>
function display()
{
var pass = string(document.registration.password.value);
var confirmpass = sring(document.registration.confirmpassword.value);

if (pass != confirmpass)
{
alert(&quot;Password and Confirm Password must be the same&quot;);
return false;
}
else
{
alert('That is correct, form will submit');
return true;
}

If (isNaN(document.registration.phonenumber.value)==true)
{
alert(&quot;Phone number must be numbers&quot;);
return false;
}
else
{
alert('That is correct, form will submit');
return true;
}

If (isNaN(document.registration.age.value)==true)
{
alert(&quot;Age must be in number&quot;);
document.registration.age.focus();
return false;
}
else
{
alert('That is correct, form will submit');
return true;
}

If (isNaN(document.registration.zip.value)==true)
{
alert(&quot;Zip must be number&quot;);
return false;
}
else
{
alert('That is correct, form will submit');
return true;
}
return true;
}
</script>
</head>
<body>
<form method = &quot;POST&quot; name=&quot;registration&quot; action = &quot; onSubmit = &quot;return display()&quot;>
Username&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;username&quot; size=&quot;30&quot;><br>
Password&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;password&quot; size=&quot;30&quot;><br>
Confirm Password&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;confirmpassword&quot; size=&quot;30&quot;><br>
Age&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;age&quot; size=&quot;2&quot;><br>
Sex&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name=&quot;sex&quot; size=&quot;1&quot;><br>
<option>Female
<option>Male
</select><br>
Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;address&quot; size=&quot;30&quot;><br>
Phone Number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;phonenumber&quot; size=&quot;30&quot;><br>
Country&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name=&quot;country&quot; size=&quot;1&quot;><br>
<option>Tanzania
<option>Kenya
<option>Uganda
<option>Zambia
<option>Zimbabwe
<option>Mozambique
</select><br>
Zip Code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type =&quot;text&quot; name=&quot;zip&quot; size=&quot;30&quot;><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=&quot;reset&quot; value=&quot;Reset&quot;>
</form>
</body>
</html>
 
i think your function should read something like:
Hope this helps

function display()
{
var pass = string(document.registration.password.value);
var confirmpass = string(document.registration.confirmpassword.value);
var msg = &quot;&quot;;

if (pass != confirmpass)
{
msg +=&quot;Password and Confirm Password must be the same&quot;;
}

if (isNaN(document.registration.phonenumber.value)==true)
{
msg +=&quot;Phone number must be numbers\n&quot;;
}

if (isNaN(document.registration.age.value)==true)
{
msg +=&quot;Age must be in number\n&quot;;
}

if (isNaN(document.registration.zip.value)==true)
{
msg +=&quot;Zip must be number\n&quot;;
}
if( msg.length == 0){
myForm.submit();
}
else{
alert(msg);
return false;
}
}
 
mit99mh; it is the same even that did not help.
Is there any other way I can do these validations?
 
Here is a slightly rewritten function for you:

function display()
{
var isOK = true;

var pass = document.registration.password.value;
var confirmpass = document.registration.confirmpassword.value;

if (pass != confirmpass)
{
alert(&quot;Password and Confirm Password must be the same&quot;);
isOK = false;
}

if (isNaN(document.registration.phonenumber.value)==true)
{
alert(&quot;Phone number must be numbers&quot;);
isOK = false;
}

if (isNaN(document.registration.age.value)==true)
{
alert(&quot;Age must be in number&quot;);
document.registration.age.focus();
isOK = false;
}

if (isNaN(document.registration.zip.value)==true)
{
alert(&quot;Zip must be number&quot;);
return false;
}

return isOK;
}

Your if statements should not have a capital I.
if is if not If

Also, your function would submit straight away if the first condition was met because you were returning true then. This will exit the function and return to the caller. Better to send it back at the end like my example.

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Here is a slightly rewritten function for you:

function display()
{
var isOK = true;

var pass = document.registration.password.value;
var confirmpass = document.registration.confirmpassword.value;

if (pass != confirmpass)
{
alert(&quot;Password and Confirm Password must be the same&quot;);
isOK = false;
}

if (isNaN(document.registration.phonenumber.value)==true)
{
alert(&quot;Phone number must be numbers&quot;);
isOK = false;
}

if (isNaN(document.registration.age.value)==true)
{
alert(&quot;Age must be in number&quot;);
document.registration.age.focus();
isOK = false;
}

if (isNaN(document.registration.zip.value)==true)
{
alert(&quot;Zip must be number&quot;);
isOK = false;
}

return isOK;
}

Your if statements should not have a capital I.
if is if not If

Also, your function would submit straight away if the first condition was met because you were returning true then. This will exit the function and return to the caller. Better to send it back at the end like my example.

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Oops, use the second one.

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Thank you so much Helltel. Is there a way to do it on insert of each field?
 
You could call a separate validation function for each field by doing something like this:
<input type =&quot;text&quot; name=&quot;confirmpassword&quot; onchange=&quot;valPass()&quot; size=&quot;30&quot; ID=&quot;Text3&quot;>

and then having each function like this:

function valPass(){
var pass = document.registration.password.value;
var confirmpass = document.registration.confirmpassword.value;

if (pass != confirmpass)
{
alert(&quot;Password and Confirm Password must be the same&quot;);
isOK = false;
}
}

var isOK would have to be defined outside of all functions to make it global.
Then you'd have to check for the value of isOK in a validation function that executes when the form is submitted. But you'd also then have to check for empty fields.

It'd be more of a pain to do it that way, but is possible. If you don't like all the alerts, try concatenating the messages into a string, similar to mit99mh's method above and displaying the whole message at once.

Make sense? If not, give us a shout again.

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top