PDA

View Full Version : Form E-Mail Validation Help..



sal007
11-15-2006, 09:06 PM
Hi people,

Just struggling a little with some scripting..

I've got this form with 3 fields(Name, Email, Idea) and i've currently got some javascript validating it, to make sure that each field has text entered within it.


<script language="JavaScript" type="text/javascript" >
function validate_form ( )

{



valid = true;
if ( document.comp.name.value == "" )
{
alert ( "please provide your name!" );
valid = false;
}
else
if ( document.comp.email.value == "" )
{
alert ( "please provide your email address!" );
valid = false;
}
else
if ( document.comp.idea.value == "" )
{
alert ( "please provide an idea for our student accommodation site" );
valid = false;
}
return valid;





}
</script>


I call this in my form, and it works fine..


<form name="comp" action="sendmail.php" method="post" onsubmit="return validate_form ( );" >


However, I wish to add some extra validation to the "email" field. I've seen plenty of email validation scripts around, but i'm not sure how to incorporate it, as it will be two different functions altogether. I guess I sound like a noob, i'm just not very skilled up when it comes to javascript!

Any help appreciated!

Cheers,

Sal

Acey99
11-15-2006, 11:25 PM
if ( document.comp.email.value == "" || NEW condition || ... )

sal007
11-16-2006, 11:45 AM
if ( document.comp.email.value == "" || NEW condition || ... )

Cheers for that matey, if i'm right that's the OR operator in an IF statement? I have tried this, but maybe i've been botching it up somewhere..

I'd be really grateful if you could show me how to incorporate the IF statement with OR, with the E-Mail validation script provided by Dynamic Drive.


http://www.dynamicdrive.com/dynamicindex16/emailvalidate.htm


<script type="text/javascript">

/***********************************************
* Email Validation script- Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e){
var returnval=emailfilter.test(e.value)
if (returnval==false){
alert("Please enter a valid email address.")
e.select()
}
return returnval
}

</script>


Many thanks,

Sal

djr33
11-16-2006, 12:28 PM
<script type="text/javascript">

/***********************************************
* Email Validation script- Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function validate_form ( )

{

valid = true;
if ( document.comp.name.value == "" )
{
alert ( "please provide your name!" );
valid = false;
}
else
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
var returnval=emailfilter.test(document.comp.email.value);
if (returnval==false){
alert("Please enter a valid email address.");
valid = false;
if ( document.comp.email.value == "" || valid == false )
{
alert ( "please provide your VALID email address!" );
valid = false;
}
else
if ( document.comp.idea.value == "" )
{
alert ( "please provide an idea for our student accommodation site" );
valid = false;
}
return valid;

}

</script>

sal007
11-16-2006, 10:54 PM
Cheers for that mate, unfortunately it didn't work though. I think a "}" was missed to close the 1st IF statement for the email condition. However, I corrected that, but it doesn't work.

All the fields are blank, and when I press the button, the popup appears "please provide your name" and the form just submits.

Any additional help really appreciated!

Regards,

sal