...

View Full Version : Help with simple js Form Validation



orit_v2003
10-08-2008, 03:08 PM
Hi,
I have a simple form.
I checked if the mail is valid and if they filled the name field.

The problem is that i want to let the client to fill their phone or mobile number.
I did a flag but it doesn't work. something is realy wrong with the code and i don't know why?




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>

<!-- Generated by responder.co.il -->
<script language="JavaScript" type="text/javascript">
<!--
function responder_validation()
{
var flag = false
var rspform = document.getElementById("rspform");
var listitems= rspform.getElementsByTagName("input");



for (a=0;a<listitems.length;a++)
{ switch (listitems[a].name)
{

case 'fields[subscribers_email]': var reEmail = /^[-\w\d\.]+@[-\w\d\.]+\.[-\w\d\.]+$/;
if(listitems[a].value == "")
{ alert("please fill in your email");listitems[a].focus();return false;}
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(listitems[a].value)))
{ alert("your email is not correct.");listitems[a].focus();return false;}
break;
case "fields[subscribers_name]": if(listitems[a].value == "")
{ alert("Please fill in your name");listitems[a].focus();return false;}
break;



case "fields[phone]": if(listitems[a].value != "")
{ flag=true;}

case "fields[mobile]": if(listitems[a].value != "")
{ flag=true;}


case "fields[subject]":
if(listitems[a].value == "")
{ alert("Please right the subject");listitems[a].focus();return false;}
break;


}
}


if (flag==false)
{
alert("Please fill in your Home phone or mobile number");
return false;

}

}
//-->
</script>

</head>

<body>
<form action="http://cp.responder.co.il/subscribe.php" method="post" onSubmit="return responder_validation()" id="rspform" >
<table id="tform" align="right" cellpadding="0" cellspacing="5" border="0">
<tr>
<td id="label">first name:</td>
<td id="control"><input id="inputText" name="fields[subscribers_name]" type="text" maxlength="20"></td>
</tr>
<tr>
<td id="label">family name:</td>
<td id="control"><input id="inputText" name="fields[family_name]" type="text" maxlength="30" /></td>
</tr>
<tr>
<td id="label">email:</td>
<td id="control"><input id="inputText" name="fields[subscribers_email]" type="text" maxlength="50" /></td>
</tr>

<tr>
<td id="label">phone:</td>
<td id="control"><input id="inputPhone" name="fields[phone]" type="text" maxlength="20" /> </td>
</tr>

<tr>
<td id="label">mobile:</td>
<td id="control"><input id="inputPhone" name="fields[mobile]" type="text" maxlength="20" /> </td>
</tr>

<tr>
<td id="label">subject:</td>
<td id="control"><input id="inputText" name="fields[subject]" type="text" maxlength="50" /></td>
</tr>

<tr>
<td id="label">comments:</td>
<td id="control"><textarea id="inputTextArea" name="fields[comments]"></textarea></td>
</tr>
<tr>
<td id="label"></td>
<td id="control"><input type="submit" value="send">

<input type="reset" name="clear" value="clear" id="inputButton"></td>
</tr>
</table>

<!--<input type="hidden" name="form_id" value="">-->
</form>
</body>
</html>


There is also problem with the focus.

Help...

shyam
10-08-2008, 04:46 PM
the missing breaks for fields[phone] and fields[mobile]

orit_v2003
10-10-2008, 11:50 AM
It was a stupid mistake and i didn't see it.
I was looking on it for 3 days.
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum