Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-23-2005, 09:31 AM   PM User | #1
kinjohn
New to the CF scene

 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kinjohn is an unknown quantity at this point
Question Can anyone plz help me correct my form validation Javascript plz ?

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
kinjohn is offline   Reply With Quote
Old 03-23-2005, 10:15 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Make the 2nd to the last if statements as else if.

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

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

}
...
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 03-24-2005, 03:17 AM   PM User | #3
kinjohn
New to the CF scene

 
Join Date: Mar 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kinjohn is an unknown quantity at this point
Thx alot gleen
kinjohn is offline   Reply With Quote
Old 03-24-2005, 07:10 AM   PM User | #4
tboss132
Regular Coder

 
Join Date: Oct 2004
Location: In front of this computer. (Where else?)
Posts: 442
Thanks: 0
Thanked 0 Times in 0 Posts
tboss132 is an unknown quantity at this point
Another way is to put a
Return false
everytime the conditions are not met like...

Code:
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
Code:
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.
__________________
Tomorrow is the first day of the rest of your life... What have you done today?
Tutorials
Web: w3schools, htmldog General tips: Hardware, Networking

Last edited by tboss132; 03-24-2005 at 08:02 AM..
tboss132 is offline   Reply With Quote
Old 03-24-2005, 07:58 AM   PM User | #5
shibby1011ph
New Coder

 
Join Date: Oct 2002
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
shibby1011ph is an unknown quantity at this point
my way of form validation

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);">
shibby1011ph is offline   Reply With Quote
Old 03-24-2005, 08:40 AM   PM User | #6
hbmarar
New Coder

 
Join Date: Mar 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
hbmarar is an unknown quantity at this point
Hope this might be informative

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
hbmarar is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 PM.


Advertisement
Log in to turn off these ads.