SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript function verify not working as hopped!

    Anyone know whats wrong with the following javascript. Verify() should
    check the other two functions and only submit the form if both return true however the form submits no matter what is entered in the "confirmnum" textbox (should be digits only)



    <%@Language ="VBScript"%>
    <%option explicit%>

    <!-- #include file = "header.html" -->
    <html>
    <head>
    <title>Cancel Details Page</title>
    <link rel=stylesheet type=text/css href="style.css">
    <style>
    img{border-style:none}
    td{padding-right:5}
    td{padding-left:15}
    table{align:center}
    </style>

    <script language="javascript">
    function isEmail(str) {
    // are regular expressions supported?
    var supported = 0;
    if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported)
    return (str.indexOf("." ) > 2) && (str.indexOf("@" ) > 0);
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)" );
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$" );
    return (!r1.test(str) && r2.test(str));
    }

    function validateOrderNumber(confirmnum)
    {
    var GoodChars = "0123456789 "
    var i = 0

    //check if empty, do a trim to ensure string of spaces will fail
    if (rightTrim(confirmnum)=="" ) {
    alert("Please specify a phone number." );
    return 0;
    }
    //check if valid
    for (i =0; i <= confirmnum.length -1; i++)
    {
    if (GoodChars.indexOf(confirmnum.charAt(i)) == -1) {
    alert("the order confirmation number you provided contains an illegal character: " + confirmnum.charAt(i));
    return 0;
    }
    }
    return 1;
    }
    function Verify()
    {
    var errorFields='';
    var errorMessage='';
    var email=canceldetails.email.value;
    var confirmnum=canceldetails.confirmnum.value;

    //canceldetails.submitButton.disabled = true;
    //Check for corectness of the input
    // if(!validateOrderNumber(confirmnum)) {
    // canceldetails.confirmnum.focus();
    // event.returnValue=false;
    // }


    if (!isEmail(email)) {
    event.returnValue=false;
    errorFields+=' .Email Address\n';
    }


    //Build error message and display it.
    if (errorFields!='') {
    errorMessage+=' The following fields are incorrect:\n';
    errorMessage+=errorFields;
    alert(errorMessage);
    canceldetails.submitButton.disabled = false;
    }

    }

    </script>

    </head>
    <body>
    <form name="canceldetails" method="POST" action="cancelConfirm.asp" onSubmit ="Verify(); return true">
    <h3>Please provide us with these order details</h3>
    <p>Email Address:
    <input type=textbox name ="email" maxlength="50">
    </p>
    <p>Order Confirm No.
    <input type=textbox name ="confirmnum" length="50">
    </p>
    <br>
    <p>
    <input type="submit" name="submitButton" value="Cancel my booking now!">
    </p>

    </form>
    </body>
    </html>

  2. #2
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to re-write your onSubmit attribute to this:

    Code:
    onSubmit="return Verify()"
    This tells the browser to ONLY return the Verify() function -- and only if the Verify() function runs through fine, is the only time the form submits.

    With this though, you must make sure you write return false; statements in your Verify() function each time after the script finds an error -- this will stop the Verify() function from completing, thus will stop the form from submitting.
    Last edited by gregc; Mar 7, 2003 at 12:42.


Bookmarks

Posting Permissions

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