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,890 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 14 m 9 sec
Reputation Power: 10
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: 138 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 14 m 55 sec
Reputation Power: 5
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: 4
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: 138 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 14 m 55 sec
Reputation Power: 5
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: 4
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


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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek