...

View Full Version : javascript validation not stopping submitting



esthera
12-28-2011, 01:23 PM
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()">

DaveyErwin
12-28-2011, 02:27 PM
<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>

chump2877
12-28-2011, 02:33 PM
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...

esthera
12-28-2011, 02:35 PM
thanks for your help

esthera
12-28-2011, 02:43 PM
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

chump2877
12-28-2011, 02:49 PM
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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum