New to JavaScript

This is a discussion on "New to JavaScript" within the JavaScript Forum section. This forum, and the thread "New to JavaScript are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 6th, 2006, 21:37
Syd Syd is offline
New Member
Join Date: Nov 2006
Location: Auckland
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
New to JavaScript

Hi.

I am new to JavaScript and to this forum.

I hope somebody can help me with this Script:

I have a page with a form validator. It works fine. However, I have 2 forms on this page and want to use the validation script for either form 1 or form 2. The fields on the form have different names.

Here is the script ::


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function validate() {

var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage


// make sure field is not blank
if (document.form1.First_Name.value=="") {
theMessage = theMessage + "\n --> Your First Name";
}

// If no errors, submit the form
if (theMessage == noErrors) {
return true;

} else {

// If errors were found, show alert message
alert(theMessage);
return false;
}
}
// End -->
</script>


Question::
In my form 2 I have a field called "Friends_First_Name". What do i have to do that the script works either on form 1 or form 2?

Any help would be great.

Regards
Syd
Reply With Quote

  #2 (permalink)  
Old Nov 6th, 2006, 22:04
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: New to JavaScript

Establish which forms 'submit' button caused the routine to be called and use if...else statements. E.g., below:
Code: Select all
if (form1){
   if (document.form1.First_Name.value=="") {
   theMessage = theMessage + "\n --> Your First Name";
   }
}
else {
   if (document.form2.Friends_First_Name.value=="") {
   theMessage = theMessage + "\n --> Your First Name";
   }
}
I'll let you have a think aboout how you determine which form's submit button called the function.
Reply With Quote
  #3 (permalink)  
Old Nov 6th, 2006, 22:14
Syd Syd is offline
New Member
Join Date: Nov 2006
Location: Auckland
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: New to JavaScript

Many thanks Geoff.

I'll try that and came back later.
Syd
Reply With Quote
  #4 (permalink)  
Old Nov 7th, 2006, 00:53
Syd Syd is offline
New Member
Join Date: Nov 2006
Location: Auckland
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: New to JavaScript

Thanks again, I have solved the problem:

Each form is using the same validation function; I gave the 2 functions different names so each for accesses the appropriate one!

Cheers
Syd
Reply With Quote
Reply

Tags
function validate

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
php and javascript yvettesio JavaScript Forum 8 Mar 14th, 2007 23:18
JavaScript cbrams9 JavaScript Forum 1 Sep 20th, 2006 17:35
using xml in javascript shailu JavaScript Forum 0 Jul 25th, 2006 07:36
Can someone help me with this javascript Galaxyblue JavaScript Forum 2 Mar 11th, 2004 12:18
what does \\ mean in javascript jenjen1018 JavaScript Forum 5 Jan 6th, 2004 17:05


All times are GMT. The time now is 10:20.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43