Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript check before posting the form (including a 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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Try this:-

    Code:
    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 = "";
    obj.focus(); 
    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.
    Last edited by Philip M; 07-18-2009 at 05:42 PM.


  •  

    Posting Permissions

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