www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript check before posting the form (with botdetect check)

  1. #1
    Join Date
    Dec 2007
    Posts
    39

    javascript check before posting the form (with botdetect check)

    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. :
    ----------------------------------------------

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

    <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.");
    document.SampleForm.Email.focus();
    return false;
    }

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

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

    </script>

    <body>
    <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") %>"/>
    </div>
    </fieldset>
    <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>

    <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>
    </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>

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

    -----------------------------------------------

    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.

  2. #2
    Join Date
    Jul 2009
    Location
    Tennessee
    Posts
    28
    Your control() function should call the isValidEmail() function. Additionally, you should be returning true in your control() function whenever the form input is valid. Really, your control() function could look like this:

    Code:
    function control() {
    
        if (document.SampleForm.Email.value == "") {
            alert("Please enter your e-mail address.");
            document.SampleForm.Email.focus();
            return false;
        }
    
        return isValidEmail(document.forms[0].Email);
    }
    I hope that helps!

  3. #3
    Join Date
    Dec 2007
    Posts
    39

    it definitely helps!

    This definitley helps.
    Thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles