5

Hello again everyone i am working on this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Form</title>
<script type="text/javascript">
</script>
</head>
<body>
<h2>**Form**</h2>
<form name=form method="post" action="javascript" subject=RSVP" enctype="text/plain" >
<input type=text name="first" size="20"> First Name<BR>
<input type=text name="last" size="20"> Last Name<BR>
<input type="text" name="email" size="20"> E-Mail<BR><BR>
<input type="submit" value="Submit">
<input type="reset" value="Clear Form"><br>
</form>
 </body>
 </html>

I am getting really confused here.. I need to have a onsubmit form handler and a create validation script. Ok now if I am right the validation script is the the function that needs to be placed right? sorry i know some of you guys might think this is easy but I am still learning. Now I have examples in my book of it but they only due one at a time. Is there a way you can do a onsubmit of all or does it have to be one at a time? thanks

ok I have this one i am working on..

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 <title>Form</title>
 <script type="text/javascript">
 <!--
 function validate_form()
 {
valid = true;
 if ( document.contact_form.contact_first.value == "" )
 {
 alert ( "Please fill in the 'Your Name' box." );
 valid = false;
 }
 return valid;
 }
 //-->
 </script>
 </head>
 <body>
 <h2>**Form**</h2>
 <form name="contact_form" method="post" action="javascript" onSubmit="return validate_form();"> 
 <input type=text name="contact_first" size="20"> First Name<BR>
 <input type=text name="contact_last" size="20"> Last Name<BR>
 <input type="text" name="contact_email" size="20"> E-Mail<BR><BR>
 <input type="submit" value="Submit">
 <input type="reset" value="Clear Form"><br>
 </form>
 </body>
</html>

now Can i just copy the function for the other two or how do i do the one for the email?

asked May 5, 2011 at 12:04
1
  • possible duplicate of working on a onsubmit Commented May 5, 2011 at 12:10

4 Answers 4

7

I've added an example here of how to do it:

http://jsfiddle.net/tomgrohl/JMkAP/

I added an onsubmit handler to the form:

<form method="post" action="javascript" enctype="text/plain" onsubmit="return valForm(this);">

And added this at the top of the page, a simple validation function:

<script type="text/javascript">
function valForm( form ){
 var firstVal, lastVal, emailVal, error = '';
 firstVal= form.first.value; 
 lastVal= form.last.value;
 emailVal= form.email.value;
 //OR
 //firstVal= document.getElementById('first').value; 
 //lastVal= document.getElementById('last').value;
 //emailVal= document.getElementById('email').value;
 if( firstVal.length == 0){
 error += 'First name is required\n';
 }
 if( lastVal.length == 0){
 error += 'Last name is required\n';
 }
 if( emailVal.length == 0){
 error += 'Email is required\n';
 }
 if( error ){
 alert( error );
 return false;
 }
 return true;
}
</script>
answered May 5, 2011 at 12:27
Sign up to request clarification or add additional context in comments.

Comments

1

OnSubmit is invoked once for the form.

You can validate all the form fields in one onSubmit function, during one call to that function.

function myOnSubmitHandler(theForm) { 
 if (theForm.data1.value == "") { 
 alert("This field is empty.");
 return false; // suppress form submission
 } else {
 return true; // A-OK, form will be submitted
 }
}

in HTML:

<form method="POST" ACTION="SomethingOnServer.php" 
 onSubmit="return myOnSubmitHandler(this);">
answered May 5, 2011 at 12:07

1 Comment

Ok such as how will you be able to give an example?
0

I need to have a onsubmit form handler

You said it; <form onsubmit="return myValidate(this);" .... >

myValidate is your validation function that returns true|false indicating whether or not you want the form to be submitted to its handler script (which your also missing).

answered May 5, 2011 at 12:07

Comments

0

might I suggest you use jQuery and jQuery validate to validate your form no need to re-invent the wheel

be sure to check out validator's demo

answered May 5, 2011 at 12:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.