View Full Version : javascript check before posting the form (including a botdetect check)

07-18-2009, 03:20 PM
Hi everybody,
I have an .asp page for my customers which does just accepts e-mail information and passes this information to another .asp page. But before passing the info, a javascript checks whether the entered e-mail does match some certain rules.

Rule1: If any input has been made at all,
Rule2: If an "@" sign is there, if the"." is there and so on.

upon pressing the submit button, the first rule works but the second rule does not. Could not figure it out why.

Here is the code. :

<!-- #include file="include/common.asp" -->
<link type='text/css' rel='Stylesheet' href='http://localhost/FormStyle.css' />
<script type="text/javascript" src="BotDetectScript.js"></script>

<SCRIPT SRC="language-tr.js"></SCRIPT>
<SCRIPT src="mainscript.js"></SCRIPT>

<script language="javascript">

if (top.frames['mainsubframe'] == undefined){
window.location.href = "index.htm";

function control()
if (document.SampleForm.Email.value == ""){
alert("Please enter your e-mail address.");
return false;

//if (!isValidEmail(document.forms[0].Email)) return false;

function isValidEmail(obj)
if (obj.value.indexOf ('@',0) == -1 obj.value.indexOf ('.',0) == -1 obj.value == "")
alert("Invalid e-mail address. Please check again.");
return false;
return true;


<form name="SampleForm" id="SampleForm" method="post" action="sendpassword.asp">
<fieldset id="SampleFields">
<legend>Sample input form</legend>
<div class="input">
<label for="Email">E-mail&nbsp;&nbsp;&nbsp;&nbsp; address</label>
<input name="Email" id="Email" type="text" class="textbox" value="" size="20" value="<% =Request("Email") %>"/>
<fieldset id="CaptchaValidation">
<legend>CAPTCHA Validation</legend>
<div id="PromptDiv">Retype the code from the picture</div>
<div id="CaptchaDiv">
<div id="CaptchaImage">
<img id="SampleForm_CaptchaImage" src="http://localhost/LanapBotDetectHandler.asp?Command=CreateImage&TextStyle=2&ImageWidth=150&imageHeight=50&CodeLength=5&CodeType=0" alt='CAPTCHA Code Image' />

<div id="CaptchaIcons">
<a href='LanapBotDetectHandler.asp?Command=CreateSound' onclick='LBD_LoadSound("SampleForm_SoundPlaceholder", "LanapBotDetectHandler.asp?Command=CreateSound");return false;' title="Speak the code"><img src="speaker.gif" alt="Speak the code" /></a>
<a href='#' onclick='LBD_ReloadImage("SampleForm_CaptchaImage"); return false;' title="Change the code"><img src="reload.gif" alt="Change the code" /></a>
<div id='SampleForm_SoundPlaceholder' class="placeholder">&nbsp;</div>

<div class="input">
<label for="CaptchaCode">Code:</label>
<input name="CaptchaCode" id="CaptchaCode" type="text" class="textbox" onkeyup="this.value = this.value.toLowerCase();" size="20" />

<div id="ActionDiv">
<input type="submit" name="ProcessForm" value="Process Form" id="ProcessForm" onclick="return control()";>


It is the function control() which validates the e-mail field. But it just validates whether the e-mail has been entered or not. The second check (with the @ sign) is not being made.
Thanks for all your comments.

Philip M
07-18-2009, 04:28 PM
Try this:-

function isValidEmail(obj) {
if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(obj.value))) {
alert ("Invalid e-mail address. Please enter again.");
obj.value = "";
return false;
return true;

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

Validation of the type if (document.SampleForm.Email.value == ""){ is barely worthy of the name and virtually worthless, as even a single space or a ? will return false (i.e. pass the validation).

BTW, please follow the posting guidelines and wrap your code in [code] tags. This means use the octothorpe or # button on the toolbar which will insert opening [ code ] and closing [ /code ] tags - omit the spaces. You can edit your previous post.