/    Sign up×
Community /Pin to ProfileBookmark

Thanks in advance…
I have a form which I’m trying to validate certain required fields which works fine. I also have a function that validates the email address in the form. Is there a way to run both functions at the same time, using the onSubmit event ( like onSubmit=”function1(); function2()”)

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterMar 31.2005 — Because to stop a form from submitting, you need to return false, you can't call

two at once like that.

[color=red]onSubmit="function1()"[/color] is not enough to stop a form from submitting.

You need to actually return it like so:

[color=red]onSubmit="[b][color=blue]return[/color][/b] function1()"[/color]

(assuming that function1() will return false)

So you will need to make a third function read something like so:

function function3(){

if(!function1())return false

if(!function2())return false

return true

}

and call it like so:

<FORM onsubmit="return function3()">
Copy linkTweet thisAlerts:
@js_prof_consMar 31.2005 — onsubmit="return (function1() && function2())" That?
Copy linkTweet thisAlerts:
@lukeurtnowskiauthorApr 01.2005 — thank you you 2, both your solutions worked
Copy linkTweet thisAlerts:
@coldboy8199May 15.2005 — Because to stop a form from submitting, you need to return false, you can't call

two at once like that.

[color=red]onSubmit="function1()"[/color] is not enough to stop a form from submitting.

You need to actually return it like so:

[color=red]onSubmit="[b][color=blue]return[/color][/b] function1()"[/color]

(assuming that function1() will return false)

So you will need to make a third function read something like so:

function function3(){

if(!function1())return false

if(!function2())return false

return true

}

and call it like so:

<FORM onsubmit="return function3()">[/QUOTE]


i have the same problem with the guy started this topic but i dunno/cant solve as what Ultimater said earlier. i'll show my javascript here, can u help me to fix it ? i'm noob in programming, now just keeping up...

My javascript codes functions :-

function setV(f){

f.elements['TotalList2Order'].value=parent.totalList2;

f.elements['TotalList3Order'].value=parent.totalList3;

f.elements['total'].value=total;

return true;

}

function checkFields() {

missinginfo = "";

if (document.order.name.value == "") {

missinginfo += "n - Name";

}

if ((document.order.email.value == "") ||

(document.order.email.value.indexOf('@') == -1) ||

(document.order.email.value.indexOf('.') == -1)) {

missinginfo += "n - Email address";

}

if (missinginfo != "") {

missinginfo ="_____________________________n" +

"You failed to correctly fill in your:n" +

missinginfo + "n_
__
__________________________" +

"nPlease re-enter and submit again!";

alert(missinginfo);

return false;

}

else

document.order.submit.disabled=true

return true;

}
[/QUOTE]


it's not possible to return as below :-
onSubmit="return setV(this); return checkFields();"[/QUOTE]

but for my checkFields there's return false and true, how do i assign 3rd function to return true while 2 other functions return false ? i hope there's someone to help me, i need to fix this problem quite urgent...
Copy linkTweet thisAlerts:
@coldboy8199May 15.2005 — bump, can sum1 help me ?
Copy linkTweet thisAlerts:
@UltimaterMay 15.2005 — onSubmit="return [color=royalblue]([/color]setV(this)[color=royalblue]&amp;&amp;[/color]checkFields()[color=royalblue])[/color]"
Copy linkTweet thisAlerts:
@coldboy8199May 16.2005 — onSubmit="return [color=royalblue]([/color]setV(this)[color=royalblue]&amp;&amp;[/color]checkFields()[color=royalblue])[/color]" [/QUOTE]

it cant works, how then ? ?
Copy linkTweet thisAlerts:
@UltimaterMay 16.2005 — That code [i]should[/i] work, if not then your error is elsewhere.

Would you mind posting some HTML to match your functions?
Copy linkTweet thisAlerts:
@coldboy8199May 17.2005 — thanks for your help, it actually works like you said, juz my fault, som other variables wrong but i notice & fix it already, thx anyway ?
Copy linkTweet thisAlerts:
@coldboy8199May 17.2005 — anyway, i got other prob, i juz remember, can you help me how 2 validate so that i can get only numbers from the textbox, as i'm doing ordering style forms, so i want javascript to alert user if they enter alphabets, so they only enter numbers to purchase the quantity. erm, do u think can help me come up with that javascript validations ?
Copy linkTweet thisAlerts:
@coldboy8199May 17.2005 — if you can help me, if it possible to include in function checkFields(), coz it check for empty strings, can as well check for only interger inputs ?
Copy linkTweet thisAlerts:
@UltimaterMay 18.2005 — Sure post some code and I'll touch it up ?
Copy linkTweet thisAlerts:
@coldboy8199May 21.2005 — function checkInt(field)

{ num = parseInt(field.value);

if (isNaN(num))
alert( "Not a number");
else if(num != parseFloat(field.value))
alert( "Not an integer" );
else if( num <= 0 )
alert( "Not a positive number (zero excluded)");

}[/QUOTE]


above is the javascript to check and alert if it's non integer/number is entered. but from the source i found, it says to add onChange on textbox as below

<input onChange="checkInt(this);" type=text name="List10Num1Qty" size=1>[/QUOTE]

can i add the checkInt function to my this image code as below, so when this image is clicked, it will call the checkInt function as well ?

<img border="0" name="List10Num1" OnClick="add(name)" alt="Add Order!" src="images/add.gif" width="81" height="22">[/QUOTE]

can i just put the checkInt inside OnClick ? will checkInt (this) refer to the textbox i want ?
Copy linkTweet thisAlerts:
@coldboy8199May 21.2005 —  function checkInt(field)

{ num = parseInt(field.value);

if (isNaN(num))

alert( "Not a number");

else if(num != parseFloat(field.value))

alert( "Not an integer" );

else if( num <= 0 )

alert( "Not a positive number (zero excluded)");

}[/QUOTE]


i feel that the code itself is not good enuff, or mayb if i dont call the checkInt function from the image & i leave the function to that textbox onChange, but can you help me to add/edit the code above, i think by adding something like return true/false ? coz i wanted the textbox keep showing alert if non integer/number is entered, so when user click on my image to call add(name) function, it wont work unless only integer/positive number is entered. hmm....do you think i explained good enough to you ? hope you understand...
×

Success!

Help @lukeurtnowski spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...