Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Executing a function only after success validation

Question posted by: cryme (Newbie) on August 7th, 2006 01:42 PM
Im having a minor, simple problem. Basicly i have two different scripts they both work separately and together but just that both executes at the same time.

On my html page i have a form, one of the functions are that you can fill in the fields and when you press on a button you go to a new page, were all filled fields are shown.

Now i want to validate the form before i pass on the filled information to the "next page".

But right now when you click on the button, you get your form validated as normal and then you just get sent to the "next page" even if you fail to meet up the req.

Quote:
Originally Posted by
<input name="submit" value="submit" onClick="compile(); validate(this.form); return document.formSubmit;" type="button">


JS code:

Quote:
Originally Posted by
function validate(form){
var error = "";
//for each form element
for(var i=0; i<form.length; i++){
var element = form[i];
//if required
if(element.getAttribute("required") == "yes"){
//if form element if empty
if(!valid(element.value,element.getAttribute("validate"),element))
error += element.getAttribute("message") + "\r\n";
}
}
if(error != ""){
alert(error);
document.formSubmit = false;
}
else
document.formSubmit = true;
}

function valid(value,type,element){
if(value == "")
return false;

switch(type){
case "int":
if(isNaN(parseInt(value)))
return false;
break;
case "float":
if(isNaN(parseFloat(value)))
return false;
break;
case "email":
var p = value.indexOf('@');
if(p<1 || p==(value.length-1))
return false;
break;
case "checked":
if(!element.checked)
return false;
break;
default://string
break;
}
return true;
}

//gather information
//pass on to "kontakta_print.html".
function compile() {
var falt = escape(document.form1.fornamn.value) + "&" +
escape(document.form1.efternamn.value) + "&" +
escape(document.form1.gatuadress.value) + "&" +
escape(document.form1.postnummer.value) + "&" +
escape(document.form1.postort.value) + "&" +
escape(document.form1.tfnnummer.value) + "&" +
escape(document.form1.epost.value) + "&" +
escape(document.form1.meddelande.value);
location.href = "kontakta_print.html?" + falt;
}

//get information from previous form
function getInfo() {
var find = location.search;
var get = find.substring(1);
var lista = get.split("&");
lista[0] = unescape(lista[0]);
return lista;
}

//funtion for printout
function skrivUt() {
var skriv = getInfo();
document.write("<p><b>F&ouml;rnamn: </b>" + skriv[0] + "<p>");
document.write("<p><b>Efternamn: </b>" + skriv[1] + "<p>");
document.write("<p><b>Adress: </b>" + skriv[2] + "<p>");
document.write("<p><b>Postnummer: </b>" + skriv[3] + "<p>");
document.write("<p><b>Postort: </b>" + skriv[4] + "<p>");
document.write("<p><b>Telefonnummer: </b>" + skriv[5] + "<p>");
document.write("<p><b>E-post: </b>" + skriv[6] + "<p>");
for (var i=0; i<skriv.length; i++)
document.write("<input type='hidden' name='input'" + i + "' value='" +skriv[i] + "' />");
}
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
cryme's Avatar
cryme
Newbie
2 Posts
August 7th, 2006
03:10 PM
#2

Re: Executing a function only after success validation
i fixed it :) just learn how to call a function

Reply
aspcoder's Avatar
aspcoder
Newbie
1 Posts
October 7th, 2007
03:01 PM
#3

Re: Executing a function only after success validation
Quote:
Originally Posted by cryme
Im having a minor, simple problem. Basicly i have two different scripts they both work separately and together but just that both executes at the same time.

On my html page i have a form, one of the functions are that you can fill in the fields and when you press on a button you go to a new page, were all filled fields are shown.

Now i want to validate the form before i pass on the filled information to the "next page".

But right now when you click on the button, you get your form validated as normal and then you just get sent to the "next page" even if you fail to meet up the req.



JS code:


Had the same problem. I solved it by checking the name on the inputboxes. If the name in your script and the name on the inputboxes in your form is different, the script will show the error messages on the ones that are correct and the send you directly to your "next" page. If all names are correct it will work perfectly.

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,042 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors