...

View Full Version : Can anyone plz help me correct my form validation Javascript plz ?



kinjohn
03-23-2005, 10:31 AM
I have the following Java codes:

function validate_form ( )
{
valid = true;

if ( enquire.txtname.value == "" )
{
alert ( "Please fill in the ' Name' box." );
valid = false;}



if ( enquire.txtemail.value == "" )
{
alert ( "Please fill in the 'Email' box." );
valid = false;}


if ( enquire.txtphone.value == "" )
{
alert ( "Please fill in the 'Phone' box." );
valid = false;}


if ( enquire.txtsubject.value == "" )
{
alert ( "Please fill in the 'Subject' box." );
valid = false;}

if ( enquire.txtmessage.value == "" )
{
alert ( "Please fill in the 'Message' box." );
valid = false;}
return valid;
}

The purpose of this code is to validate the textboxes when the submit button is click; to see whether they are empty or not. If it is empty, a message box will come up and say that field is empty.

I can run this code alright, but it doesn't quite do waht I really want it to do. If the user leave all 5 fields empty and lcik the submit button, five message box will show up. I don't want this to happen.

The way I want it to work is if the user doesn't input anything into the 5 field, the first field ( in my case, it is the name fireld) message box will come only. If the name field is filled in and the other four aren't, the "email" message box will come up. The rest follows this logic

Can some experts help me correcting my code plz

Thx

glenngv
03-23-2005, 11:15 AM
Make the 2nd to the last if statements as else if.


else if ( enquire.txtemail.value == "" ){

}
else if ( enquire.txtphone.value == "" ){

}
...

kinjohn
03-24-2005, 04:17 AM
Thx alot gleen

tboss132
03-24-2005, 08:10 AM
Another way is to put a
Return false
everytime the conditions are not met like...


function validate_form ( )
{
valid = true;

if ( enquire.txtname.value == "" )
{
alert ( "Please fill in the ' Name' box." );
valid = false;}
return false
}

And put this attribute in your form tags

onSubmit="return validate_form();"
I prefer doing it that way because then the javascript exits immediately a condition is not met and doesn't have to go through the whole code every time.

shibby1011ph
03-24-2005, 08:58 AM
function validateForm(myForm){
var msg = "";
if(myForm.myTextBox1.value.length < 1){
msg += "please provide an appropriate value.\n";
}elseif(myForm.myTextBox2.value.length < 2){
msg += "please enter a value! \n";
}

if (msg.length >0){
alert(msg);
return false;
}else{
return true;
}
}

<form name="myForm" onsubmit="return validateForm(document.myForm);">

hbmarar
03-24-2005, 09:40 AM
function chk(f){

if (f.enq_name.value==""){
f.enq_name.focus();
alert ('Please enter your Name');
return false;
}
similair ifs for other texts validations
//


}

you can invoke this script function with the following

onClick=\"JavaScript:return chk(this.form);\"

happy to share with you and this should help you...wishing you the best



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum