Hi
I use the following form validation and it works all fine, except my email input does not get validated.

Input type = email
field name = emailto

this is the code:

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("emailto","from", "emailfrom","zigi");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Friends E-mail","Friends Name", "Your E-mail","Your Name");
	// dialog message
	var alertMsg = "These fields cannot be left blank:\n";

	var l_Msg = alertMsg.length;

	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "email":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				var strValue = emailto.obj.value; 
				var strFilter = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i;
				if (!strFilter.test(strValue))
					alertMsg = "wrong or incomplete email" + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
        }
   }
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

What am I missing in the email validation, please?

Thank a lot

Recommended Answers

All 6 Replies

Tks.
But I have no experience in using jquery for a form ... plus, this would need to redo the entire form from scratch and I worked 3 days on that form ... to get it where it is

Learning jquery would take me too long right now. But worth considering for the future, that's for sure

you wouldnt have to redo the form. Just


1) add the jquery.js and plugin files to the server
2) include the jquery.js and plugin files in your html
3) add like 3 lines of code.


that's the beauty of jquery and why it's slogan is: "WRITE LESS DO MORE"

it's designed to SAVE you time

This is the form page (.php)

<?php
include('config.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<!-- Start Colorbox -->
<link type="text/css" media="screen" rel="stylesheet" href="colorbox.css" />
<script type="text/javascript" src="lightview/jquery.min.js"></script>
<script type="text/javascript" src="lightview/jquery.colorbox.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$(".iframe").colorbox({width:"50%", height:"85%", iframe:true});

		//Example of preserving a JavaScript event for inline calls.
		$("#click").click(function(){ 
			$('#click').css({"background-color":"#f00", "color":"#000", "cursor":"inherit"}).text("Contact Zeroge Business Directory");
			return false;
		});
	});
</script>
<!-- End Colorbox -->

<title><? echo $title; ?></title>

<script language="JavaScript">


var highlightcolor="lightyellow"

var ns6=document.getElementById&&!document.all
var previous=''
var eventobj

//Regular expression to highlight only form elements
var intended=/INPUT|TEXTAREA|SELECT|OPTION/

//Function to check whether element clicked is form element
function checkel(which){
if (which.style&&intended.test(which.tagName)){
if (ns6&&eventobj.nodeType==3)
eventobj=eventobj.parentNode.parentNode
return true
}
else
return false
}

//Function to highlight form element
function highlight(e){
eventobj=ns6? e.target : event.srcElement
if (previous!=''){
if (checkel(previous))
previous.style.backgroundColor=''
previous=eventobj
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
}
else{
if (checkel(eventobj))
eventobj.style.backgroundColor=highlightcolor
previous=eventobj
}
}

</script>
<style type="text/css">
html {
  padding:0px;
  margin:0px;
}

body {
  font-family: Georgia, Serif;
  font-weight: bold; 
  padding: 0px 20px;
  margin: 0px;
}

#menu {
  font-family: Tahoma, Serif;
  font-weight: normal; 
  font-size:11px;
  color:#414A5A;
  position: absolute;
  width: 380px;
  left: 20px;
  padding: 20px;
  margin:0px;
  background: #F5F5F5;
  border: 1px dashed #000000;
}

#content {
  font-family: Arial;
  font-weight: normal; 
  font-size:10px;
  color:#414A5A;
  width:380px;
  margin-top: 95px;
  left: 20px;
  background: #F5F5F5;
  padding: 0px 10px 20px 30px;
  border: 1px dashed #000000;
}

h1 {
  font-size:large;
  padding-top: 10px;
}
</style>
</head>

<body>


<br />
<div id="menu">
<tr>Recommend this page to your friend</tr>
<tr></tr>
</div>

<div id="content">

<h1><? echo $title; ?></h1><hr><br>

<script language="JavaScript">

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("to","from", "say","zigi");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Friends E-mail","Friends Name", "Your E-mail","Your Name");
	// dialog message
	var alertMsg = "These fields cannot be left blank:\n";

	var l_Msg = alertMsg.length;

	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
        }
   }
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

</script>



<p><form name="formcheck"  method="post" action="http://chinabiz21.com/support/refer/mail.php" onsubmit="return formCheck(this) ///onKeyUp="highlight(event)" onClick="highlight(event);">

    <strong>Friends E-mail</strong><br/>
        <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="to" size="35"><br>
        
    <strong>Friends Name</strong><br/>
        <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="from" size="35"><br>
    <strong>Your E-mail</strong><br/>
        <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="say" size="35"><br>
    <strong>Your Name</strong><br/>
        <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="zigi" size="35"><br>
    <strong>Your Message</strong><br/>
        <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="msg" size="35"><br>
<p></p>
	<input TYPE="submit" NAME="Request" VALUE="Submit This Form">
     	<input TYPE="reset" NAME="Clear" VALUE="Clear Form and Start Over">

</form></p>
<p>
</p>
</div>

</body>
</html>

the mail.php page has two other includes and the whole form opens in a light box ... you can view the form sample in action here, simply click on the left rollover (refer a friend) and the form will open in the lightbox

and this is the mail.php

<?php
include('config.php');
include('mail.body.php');
$time = date ("h:i A"); 
$date = date ("l, F jS, Y");
$urlh = getenv(HTTP_HOST);
$url = "http://$urlh";
$IP = $_SERVER['REMOTE_ADDR'];
$to = $_POST['to']; 
$subject = $_POST['subject'];
$say = $_POST['say'];
$from = $_POST['from'];
$msg = $_POST['msg'];
$zigi = $_POST['zigi'];
$body ='
<html>
<head>
<style>
<!--
body, P.msoNormal, LI.msoNormal
{

background-position: top;
background-color: #ffffff;
margin-left:  2em;
margin-top: 0em;
margin-bottom: 4em;
font-family: "verdana";
font-size:   9pt;
font-weight: ;
color:    "000000";



}
-->
</style>
</head>
</body> 
';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Chinabiz21.com Admin <me@myurl.com>' . "\r\n";
$bodys .= ("
$zigi found something on $site which you might find
interesting. Click on this <a href=$url>link</a> to view the information, or click <a href=mailto:$say>here</a> to reply $zigi on this message.<br><br>$zigi also left this private message for you:<hr>
");
$bodys .= "<b><font color=#2B60DE>$msg</font><b><br><hr>";
$bodys .= "<li>The message was left on: $date, $time <br>";
$bodys .= "<li>Recommended Site URL: $url";
$subject .= " $from - $zigi recommends $site";
$body = $body . $bodys . $message;
mail($to, $subject ,$body, $headers);
?>
<?php
include('config.php');
$urlh = getenv(HTTP_HOST);
$url = "http://$urlh";
$email;
$date = date ("l, F jS, Y"); 
$time = date ("h:i A");
$subject = "Info";
$body = $_POST['body'];
$bodys = "$title\nMessage:\n$msg\nThis mesage sent to $form was submitted on\n $date at\n $time.\n From IP Address.$IP \n From E Address\n$say\nTo E Address\n $to. \nUrl.\n $url "; 
$IP = $_SERVER['REMOTE_ADDR'];
$body = $body . $bodys;
mail($email, $subject, $body, "From: $email");
?>
<html>
<head>
<style>
<!--
body, P.msoNormal, LI.msoNormal
{

background-position: top;
background-color: #ffffff;
margin-left:  2em;
margin-top: 0em;
margin-bottom: 4em;
font-family: "verdana";
font-size:   9pt;
font-weight:  bold;
color:    "#151B8D";



}
-->
</style>
</head>
</body> 

<script type="text/javascript">
<!--
var count = 4; // currently set to 5 seconds - change as necessary

function counter() // this function must be called in the html document as follows: <body onload="counter()">
{
	count--;
	
	if(count == 0) 
	{ 
		window.location = "/"; // enter the location that you wish to redirect visitors to
	}

	setTimeout("counter()", 1000); // timeout function invokes and loops the function every second.
}
//-->
</script>
</head>
<body onLoad="counter()"> <!-- function must be invoked when the page loads //-->
<center><strong><p>Thank you! Your email has been sent to your friend. Redirection....</p></strong></center>
</body>
</html>

SO REALLY, I WOULD NOT HAVE A CLUE WHERE AND HOW TO PLACE THE LIVE-VALIDATION SO IN ORDER TO KEEP THE FORM FUNCTIONAL ...

cCheers

well best of luck to ya then.

Sure then, ya absolutely

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.