JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 23rd, 2006, 09:53 AM
///M ///M is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 8 ///M User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 58 sec
Reputation Power: 0
Help needed please

Hi there. I am using the code below to learn about form validation. When I insert correct data the form sends the data, and when i enter wrong data it clears the fields. What i am ideally looking for is for the code to keep the data on screen so that I can change it if i make a mistake. Here is the code below.

Thanks in advance for your help


<script language="JavaScript1.2">

function CheckName(HoldName)
{
NoNumThere=true;

for(i=0; i<HoldName.length; i++)
{

for(j=0; j<10; j++)
{

if(HoldName.charAt(i)==j.toString())
{

NoNumThere=false;
break;

}

}

if(NoNumThere==false)
{

break;

}

}

return NoNumThere;

}

function CheckMail(HoldMail)
{

IsValid=true;

if(HoldMail.indexOf("@")<=0)
{

IsValid=false;

}

return IsValid;

}

function checkfields()
{

var AllFilled=true;

for(i=0; i<3; i++)
{

if(visitor.elements[i].value.length==0)
{

alert("The field " + visitor.elements[i].name + " can not be left blank.");
AllFilled=false;
visitor.elements[i].focus;


}

}

if(AllFilled==true)
{

var NameValid=true;
var EmailValid=true;
NameValid=CheckName(visitor.fullname.value);
EmailValid=CheckMail(visitor.email.value);

if(NameValid==false)
{

alert("Sorry, your name can not contain numbers.");
visitor.fullname.focus;

}

if(EmailValid==false)
{

alert("Sorry, this does not seem like a valid email address.");

}


}

if(NameValid==true & EmailValid==true)
{

alert("Worked");

}

}

</script>

<body>
<form name="visitor">
Enter your name: <input name="fullname" type="Text">
<br>
Enter your email: <input name="email" type="Text">
<br>


<input type="submit" name="submit" value="Submit" onClick="checkfields();">
<input type="Reset" name="reset" value="Reset">
</form>
</body>
</html>

Allan

Reply With Quote
  #2  
Old May 24th, 2006, 04:30 AM
///M ///M is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 8 ///M User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 58 sec
Reputation Power: 0
Anyone???

Reply With Quote
  #3  
Old May 24th, 2006, 11:55 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Hi Allan! Welcome to the forums.

I had a look at your code... it looks like you might have more loops than you really need. You also may want to consider Regular Expressions because they're powerful and fun =)

However, I found a quick way to fix the code you've already written.
what I did was add some return statements after your alert messages and changed the submit button slightly.

Code:
		if(NameValid==false)
		{

			alert("Sorry, your name can not contain numbers.");
			visitor.fullname.focus;
			return false;

		}

		if(EmailValid==false)
		{

			alert("Sorry, this does not seem like a valid email address.");
			return false;

		}

	}

	if(NameValid==true & EmailValid==true)
	{

		alert("Worked");
		return true;

	}


Code:
<input type="submit" name="submit" value="Submit" onclick="return checkfields();">


Also note, in the future wrap your code with [code][/code] tags when posting... it makes it easier to read
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #4  
Old May 24th, 2006, 04:12 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
It would be better to use the form's onsubmit event than to use the submit button's onclick event.
Code:
<form name="visitor" onsubmit="return checkfields();">


Edit: I fixed it.

Reply With Quote
  #5  
Old May 24th, 2006, 10:05 PM
ravs ravs is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: gurgaon, haryana, india
Posts: 60 ravs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 29 sec
Reputation Power: 13
Send a message via Yahoo to ravs
i use


<form name="visitor" onsubmit="return checkfields();">

Reply With Quote
  #6  
Old May 24th, 2006, 10:12 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 140 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 33 m 14 sec
Reputation Power: 13
Thanks ravs. I forgot to change the "onclick" to "onsubmit" when I copy-and-pasted the event handler. I corrected my previous reply.

Reply With Quote
  #7  
Old May 25th, 2006, 12:11 AM
ravs ravs is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: gurgaon, haryana, india
Posts: 60 ravs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 29 sec
Reputation Power: 13
Send a message via Yahoo to ravs
Thumbs up

my pleasure
we all do mistakes

Reply With Quote
  #8  
Old May 25th, 2006, 08:41 AM
///M ///M is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 8 ///M User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 58 sec
Reputation Power: 0
Thanks for the welcome and the help, the program works fine now. Thank you.

Allan

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Help needed please


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap