Click to See Complete Forum and Search --> : check form


dummyagain
September 25th, 2006, 03:18 AM
I don't know why the script will not able to call the checkform function


<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>Contact Us</title>

<script language="JavaScript">
function checkForm()
{
alert("Test");
var name, email,tel,subject, comment;
with(window.document.contactus)
{
name = name;
email = email;
tel = tel;
subject = subject;
comment = comment;
}

if(trim(name.value) == '')
{
alert('Please enter your name');
name.focus();
return false;
}
else if(trim(email.value) == '')
{
alert('Please enter your email');
email.focus();
return false;
}
else if(!isEmail(trim(email.value)))
{
alert('Email address is not valid');
email.focus();
return false;
}
else if(trim(tel.value) == '')
{
alert('Please enter contact number');
tel.focus();
return false;
}
else if(trim(subject.value) == '')
{
alert('Please enter message subject');
subject.focus();
return false;
}
else if(trim(comment.value) == '')
{
alert('Please enter your message');
comment.focus();
return false;
}
else
{
name.value = trim(name.value);
email.value = trim(email.value);
subject.value = trim(subject.value);
tel.value = trim(tel.value);
comment.value = trim(comment.value);
return true;
}
}

function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(str)
{
var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|
ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|
bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|
ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|
zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

return regex.test(str);
}
</script>
</head>
<body>
<form name="contactus" method="post" action="insertc.php">
<p><B><FONT face=Arial size=2>Customer satisfaction has always been our prime objective. Your comments are most valuable to us. <br>
To help us upgrading the quality and standard of our service, please let us have your comments.</FONT></B> </p>
<table width="500" border="1">
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Tel No. </td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td colspan="2"><FONT face=Arial size=2>Please feel free to write down your comment:</FONT></td>
</tr>
<tr>
<td>Subject</td>
<td><select name="subject">
<option value="suggestion">Suggestion</option>
<option value="others">Others</option>
</select></td>
</tr>
<tr>
<td height="86" colspan="2"><textarea name="comment" cols="80" rows="15"></textarea></td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="Submit" onClick="return checkForm();">
<input type="reset" name="Reset" value="Reset">


Thank you for your advice

PeejAvery
September 25th, 2006, 09:59 AM
You can debug this situation.

If you are using IE, turn on your page debugger to see what line the error is at. If you are using Firefox, just check the JavaScript Console.

dummyagain
September 25th, 2006, 09:30 PM
I found that the problem is from here

function isEmail(str)
{
var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|
ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|
bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|
ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|
dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|
gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|
hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|
kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|
ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|
mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|
nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|
re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|
su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|
ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|
zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;

return regex.test(str);
}


Error: unterminated regular expression literal
var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|


but i don't see any mistake...can you help, please?
Thank you

HanneSThEGreaT
September 26th, 2006, 05:48 AM
Whoa! That is a looong Regular Expression! :eek:

It seems like you want to check for a valid email address.

Then, there is a much easier and less time consuming way, have a look at this :
<HTML>

<HEAD>
<TITLE>Validate Email</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide JS code
function validateForm(form) // validate Survey data
{
if (!validateEMail(form.email.value)) // email valid?
{
form.email.focus()
return false
}
alert("Congratulations: Your data is valid!") // all data valid
return true
}

function isBlank(testStr)
{
if (testStr.length == 0) // nothing entered?
return true
for (var i = 0; i <= testStr.length-1; i++) // all spaces?
if (testStr.charAt(i) != " ")
return false
return true
}
function validateEMail(email)
{
if (isBlank(email)) // email blank?
{
alert("Enter your email address, please!")
return false
}
var atsignPos = email.indexOf("@", 0) // check for @
if (atsignPos == -1)
{
alert("Enter a valid email address with an @, please!")
return false
}
if (email.indexOf(".", atsignPos) == -1) // check for . after @
{
alert("Enter a valid email domain after the @, please!")
return false
}
return true
}
// end JS hide -->
</SCRIPT>
</HEAD>

<BODY BGCOLOR="black" TEXT="white"
onLoad="window.defaultStatus='Validate Email'">
<CENTER>
<H2>Validate Email</H2>
<FORM NAME="surveyForm">
EMail: <INPUT TYPE="text" NAME="email" SIZE="26"><P>
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="button" VALUE="Validate" onClick="validateForm(this.form)">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
</CENTER>
</BODY>

</HTML>

I hope it helps! :)

PeejAvery
September 26th, 2006, 09:35 AM
Wow. Who made that regular expression. I can see strengths in it, if you were trying to find the country of the email address, but that can always be fooled.

Hannes, once again, a good job!

dummyagain
September 26th, 2006, 10:40 AM
Thank you!