+ 1

Why are we using "return" on "onsubmit"?

https://code.sololearn.com/WA25A1012a22 This is the code for form validation in JS. Can anyone explain why are we using return on "onsubmit" event? The function already returns the value.

22nd Jun 2021, 11:38 AM
NBinte
NBinte - avatar
7 Answers
+ 3
NBinte after evaluation the function spits either true or false which just evaluates to something like this onsubmit=true or onsubmit=false. but in truth onsubmit takes a function callback (after learning JavaScript you will know it).. so you need to return something ... not set the callback event to true or false .. now get it? I believe you should see for callbacks and events to get a better understanding ... have a look at these : https://www.w3schools.com/js/js_callback.asp https://www.w3schools.com/jsref/event_onsubmit.asp
22nd Jun 2021, 2:41 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 2
actually you place a JavaScript code to run in onsubmit attribute of form. and if the value returned is true , then only form gets submitted successfully , else its not. Hope you got it !?😅
22nd Jun 2021, 11:42 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 2
Okay get it. Thanks everyone. :)
23rd Jun 2021, 4:13 AM
NBinte
NBinte - avatar
+ 1
@Prashanth Calling the function like onsubmit="validate()" already gets the returned the value and it works too. My question was why we are again using onsubmit="return validate()".
22nd Jun 2021, 11:46 AM
NBinte
NBinte - avatar
+ 1
NBinte Check with and without return and enter different values in both fields then submit you will know what Prashanth Kumar wanted to say.
22nd Jun 2021, 2:15 PM
A͢J
A͢J - avatar
+ 1
NBinte If you do not use return then after returning false form will be submit.
22nd Jun 2021, 2:18 PM
A͢J
A͢J - avatar