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 6 of 6
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts

    javascript validation not stopping submitting

    I am using the following validation routine


    function savenotes() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

    var result = xmlhttp.responseText.split("~");
    alert(result[0])
    if (result[0] == "1") {

    // alert(result[1]);
    alert("should return true");
    valid = true;
    }
    else {

    alert(result[1]);
    valid = false;
    }


    }




    }
    var str = "ok";
    var mypost = "check.asp"
    // alert(mypost);
    xmlhttp.open("GET", mypost, true);
    xmlhttp.send();

    return valid;
    }




    now where do I put the return so that it returns to my form if it's valid or not.
    It doesn't seem to work to me and the form is always submitted

    I am calling this with


    <form method=post action=admin.asp onsubmit="return savenotes()">

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
    <script>
    	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    		xmlhttp = new XMLHttpRequest();
    	}else {// code for IE6, IE5
    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    
    function validateOnServer() {
    	var mypost = "check.asp"
    	xmlhttp.open("GET", mypost, false);
    	xmlhttp.send(null);
    	var result = xmlhttp.responseText.split("~");
    		alert(result[0])
    		if (result[0] == "1") {
    
    			// alert(result[1]);
    			alert("should return true");
    			valid = true;
    		}else {
    
    			alert(result[1]);
    			valid = false;
    		}
    return valid;
    }
    </script>
    
    <form method=post action=admin.asp onsubmit="return validateOnServer()"> 
    <input>
    </form>

  • Users who have thanked DaveyErwin for this post:

    esthera (12-28-2011)

  • #3
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts
    You are performing an asynchronous request to the server on form submit. Why?

    Is your aim to validate the form without refreshing the page? If not, then why bother with the AJAX?

    If your aim is in fact to validate the form, or parts of the form, asynchronously, then I would choose another event to handle (other than form submit) -- such as formElement.onblur, formElement.onchange, etc...
    Regards, R.J.

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

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #4
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    thanks for your help

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    If your aim is in fact to validate the form, or parts of the form, asynchronously, then I would choose another event to handle (other than form submit) -- such as formElement.onblur, formElement.onchange, etc...

    my aim is to validate and I want to validate on submit not after each field is filled in

  • #6
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,776
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Then have the form validated on the server side after form submit (using your ASP code) if your aim is to validate the form after it is completed.

    There's no point in running the same ASP code twice (first with AJAX on form submit and then again following form submit).

    And you "will" have to run the code twice if you are doing what you are doing, because your AJAX validation will fail if JS is turned off in the browser...And if that happens, you need to implement server side validation again anyway after form submit as a failsafe mechanism...
    Regards, R.J.

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

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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