Javascript Form validation disabled submit without any framework












0















I am trying to add my own form validation to this form. The aim is at the end to have these rules:




  • If email not valid, display an error.

  • If text area < 50 char, display an error.

  • If all inputs are invalid, disable the submit.


My first aim is to disable the submit. I tried this with many solutions found in the Internet. Unfortunately, without success. My last try is inspired by this post: https://www.plus2net.com/javascript_tutorial/form-submit-demo.php.



This is my form:



<form name="myForm" action="##" method="post"> 
<div class="margin_b">
<div class="opinion-radio margin_b">
<span class="label-left">How do you like our pizza?</span> <!-- First element of the form -->
<div class="checkbox-style inputs-right">
<input id="awesome" type="radio" name="review" value="awesome">
<label for="awesome" class="margin-right">Awesome</label>
<input id="good" type="radio" name="review" value="good">
<label for="good" class="margin-right">Good</label>
<input id="ok" type="radio" name="review" value="ok">
<label for="ok"class="margin-right">Ok</label>
<input id="poor" type="radio" name="review" value="poor">
<label for="poor">Poor</label>
</div>
<span id="error-review" class="label-left error">Please select a response.</span>
</div>
<div class="opinion-radio">
<span class="inline, label-left">What do you think about our prices?</span> <!-- Second element of the form -->
<div class="checkbox-style inputs-right">
<input id="fair" type="radio" name="reviewprice" value="fair">
<label for="fair" class="margin-right">Fair</label>
<input id="okay" type="radio" name="reviewprice" value="okay">
<label for="okay" class="margin-right">Okay</label>
<input id="expensive" type="radio" name="reviewprice" value="expensive">
<label for="expensive">Expensive</label>
</div>
<span id="error-reviewprice" class="label-left error">Please select a response.</span>
</div>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your name:</label> <!-- Third element of the form -->
<input type="text" id="name" name="user_name" class="inline inputs-right">
<span id="error-name" class="error label-left">Please add a name.</span>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your e-mail address:</label> <!-- Fourth element of the form -->
<input type="email" id="email" name="user_email" class="inline inputs-right">
<span id="error-email" class="label-left error">Please add a correct email.</span>
</div>
<div class="margin_b">
<label for="msg" class="label-left">What can we do better :</label> <!-- Fifth element of the form -->
<textarea id="msg" name="user_message" class="inline inputs-right"></textarea>
<span id="error-text" class="label-left error">Please add a correct text of at least 50 characters.</span>
</div>
<div class="align-right">
<input type="submit" value="Submit" id="btnsubmit">
</div>
</form>


This is my JS (with a first try with the first question of the form):



function formValidation() { 

t1ck=true;

if (!document.getElementById("awesome").checked && !document.getElementById("good").checked && !document.getElementById("ok").checked && !document.getElementById("poor").checked ) {
alert("false");
t1ck=false;
}

if (t1ck == false) {
var btnsubmit = document.getElementById("btnsubmit");
btnsubmit.disabled = true;
btnsubmit.style.backgroundColor = "grey";
} else {
t1ck=true;
alert("true");
btnsubmit.disabled = false;
}
}

function submitCheck() {

var awesome = document.getElementById("awesome");
var good = document.getElementById("good");
var ok = document.getElementById("ok");
var poor = document.getElementById("poor");

t1ck=false;
document.getElementById("btnsubmit").disabled = true;

awesome.onclick = formValidation();
good.onclick = formValidation();
ok.onclick = formValidation();
poor.onclick = formValidation();
}
window.onload = submitCheck;


What I discovered is that alert("false"), which is here for debugging purposes) will always be called when the page load, even if this is linked with an onclick event. Furthermore, I never achieved to trigger alert("true").










share|improve this question























  • You may want to consider reading about HTML5 validation: developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… since browsers now implement most (or maybe all) of what you want natively.

    – Daniel
    Nov 19 '18 at 23:23
















0















I am trying to add my own form validation to this form. The aim is at the end to have these rules:




  • If email not valid, display an error.

  • If text area < 50 char, display an error.

  • If all inputs are invalid, disable the submit.


My first aim is to disable the submit. I tried this with many solutions found in the Internet. Unfortunately, without success. My last try is inspired by this post: https://www.plus2net.com/javascript_tutorial/form-submit-demo.php.



This is my form:



<form name="myForm" action="##" method="post"> 
<div class="margin_b">
<div class="opinion-radio margin_b">
<span class="label-left">How do you like our pizza?</span> <!-- First element of the form -->
<div class="checkbox-style inputs-right">
<input id="awesome" type="radio" name="review" value="awesome">
<label for="awesome" class="margin-right">Awesome</label>
<input id="good" type="radio" name="review" value="good">
<label for="good" class="margin-right">Good</label>
<input id="ok" type="radio" name="review" value="ok">
<label for="ok"class="margin-right">Ok</label>
<input id="poor" type="radio" name="review" value="poor">
<label for="poor">Poor</label>
</div>
<span id="error-review" class="label-left error">Please select a response.</span>
</div>
<div class="opinion-radio">
<span class="inline, label-left">What do you think about our prices?</span> <!-- Second element of the form -->
<div class="checkbox-style inputs-right">
<input id="fair" type="radio" name="reviewprice" value="fair">
<label for="fair" class="margin-right">Fair</label>
<input id="okay" type="radio" name="reviewprice" value="okay">
<label for="okay" class="margin-right">Okay</label>
<input id="expensive" type="radio" name="reviewprice" value="expensive">
<label for="expensive">Expensive</label>
</div>
<span id="error-reviewprice" class="label-left error">Please select a response.</span>
</div>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your name:</label> <!-- Third element of the form -->
<input type="text" id="name" name="user_name" class="inline inputs-right">
<span id="error-name" class="error label-left">Please add a name.</span>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your e-mail address:</label> <!-- Fourth element of the form -->
<input type="email" id="email" name="user_email" class="inline inputs-right">
<span id="error-email" class="label-left error">Please add a correct email.</span>
</div>
<div class="margin_b">
<label for="msg" class="label-left">What can we do better :</label> <!-- Fifth element of the form -->
<textarea id="msg" name="user_message" class="inline inputs-right"></textarea>
<span id="error-text" class="label-left error">Please add a correct text of at least 50 characters.</span>
</div>
<div class="align-right">
<input type="submit" value="Submit" id="btnsubmit">
</div>
</form>


This is my JS (with a first try with the first question of the form):



function formValidation() { 

t1ck=true;

if (!document.getElementById("awesome").checked && !document.getElementById("good").checked && !document.getElementById("ok").checked && !document.getElementById("poor").checked ) {
alert("false");
t1ck=false;
}

if (t1ck == false) {
var btnsubmit = document.getElementById("btnsubmit");
btnsubmit.disabled = true;
btnsubmit.style.backgroundColor = "grey";
} else {
t1ck=true;
alert("true");
btnsubmit.disabled = false;
}
}

function submitCheck() {

var awesome = document.getElementById("awesome");
var good = document.getElementById("good");
var ok = document.getElementById("ok");
var poor = document.getElementById("poor");

t1ck=false;
document.getElementById("btnsubmit").disabled = true;

awesome.onclick = formValidation();
good.onclick = formValidation();
ok.onclick = formValidation();
poor.onclick = formValidation();
}
window.onload = submitCheck;


What I discovered is that alert("false"), which is here for debugging purposes) will always be called when the page load, even if this is linked with an onclick event. Furthermore, I never achieved to trigger alert("true").










share|improve this question























  • You may want to consider reading about HTML5 validation: developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… since browsers now implement most (or maybe all) of what you want natively.

    – Daniel
    Nov 19 '18 at 23:23














0












0








0








I am trying to add my own form validation to this form. The aim is at the end to have these rules:




  • If email not valid, display an error.

  • If text area < 50 char, display an error.

  • If all inputs are invalid, disable the submit.


My first aim is to disable the submit. I tried this with many solutions found in the Internet. Unfortunately, without success. My last try is inspired by this post: https://www.plus2net.com/javascript_tutorial/form-submit-demo.php.



This is my form:



<form name="myForm" action="##" method="post"> 
<div class="margin_b">
<div class="opinion-radio margin_b">
<span class="label-left">How do you like our pizza?</span> <!-- First element of the form -->
<div class="checkbox-style inputs-right">
<input id="awesome" type="radio" name="review" value="awesome">
<label for="awesome" class="margin-right">Awesome</label>
<input id="good" type="radio" name="review" value="good">
<label for="good" class="margin-right">Good</label>
<input id="ok" type="radio" name="review" value="ok">
<label for="ok"class="margin-right">Ok</label>
<input id="poor" type="radio" name="review" value="poor">
<label for="poor">Poor</label>
</div>
<span id="error-review" class="label-left error">Please select a response.</span>
</div>
<div class="opinion-radio">
<span class="inline, label-left">What do you think about our prices?</span> <!-- Second element of the form -->
<div class="checkbox-style inputs-right">
<input id="fair" type="radio" name="reviewprice" value="fair">
<label for="fair" class="margin-right">Fair</label>
<input id="okay" type="radio" name="reviewprice" value="okay">
<label for="okay" class="margin-right">Okay</label>
<input id="expensive" type="radio" name="reviewprice" value="expensive">
<label for="expensive">Expensive</label>
</div>
<span id="error-reviewprice" class="label-left error">Please select a response.</span>
</div>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your name:</label> <!-- Third element of the form -->
<input type="text" id="name" name="user_name" class="inline inputs-right">
<span id="error-name" class="error label-left">Please add a name.</span>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your e-mail address:</label> <!-- Fourth element of the form -->
<input type="email" id="email" name="user_email" class="inline inputs-right">
<span id="error-email" class="label-left error">Please add a correct email.</span>
</div>
<div class="margin_b">
<label for="msg" class="label-left">What can we do better :</label> <!-- Fifth element of the form -->
<textarea id="msg" name="user_message" class="inline inputs-right"></textarea>
<span id="error-text" class="label-left error">Please add a correct text of at least 50 characters.</span>
</div>
<div class="align-right">
<input type="submit" value="Submit" id="btnsubmit">
</div>
</form>


This is my JS (with a first try with the first question of the form):



function formValidation() { 

t1ck=true;

if (!document.getElementById("awesome").checked && !document.getElementById("good").checked && !document.getElementById("ok").checked && !document.getElementById("poor").checked ) {
alert("false");
t1ck=false;
}

if (t1ck == false) {
var btnsubmit = document.getElementById("btnsubmit");
btnsubmit.disabled = true;
btnsubmit.style.backgroundColor = "grey";
} else {
t1ck=true;
alert("true");
btnsubmit.disabled = false;
}
}

function submitCheck() {

var awesome = document.getElementById("awesome");
var good = document.getElementById("good");
var ok = document.getElementById("ok");
var poor = document.getElementById("poor");

t1ck=false;
document.getElementById("btnsubmit").disabled = true;

awesome.onclick = formValidation();
good.onclick = formValidation();
ok.onclick = formValidation();
poor.onclick = formValidation();
}
window.onload = submitCheck;


What I discovered is that alert("false"), which is here for debugging purposes) will always be called when the page load, even if this is linked with an onclick event. Furthermore, I never achieved to trigger alert("true").










share|improve this question














I am trying to add my own form validation to this form. The aim is at the end to have these rules:




  • If email not valid, display an error.

  • If text area < 50 char, display an error.

  • If all inputs are invalid, disable the submit.


My first aim is to disable the submit. I tried this with many solutions found in the Internet. Unfortunately, without success. My last try is inspired by this post: https://www.plus2net.com/javascript_tutorial/form-submit-demo.php.



This is my form:



<form name="myForm" action="##" method="post"> 
<div class="margin_b">
<div class="opinion-radio margin_b">
<span class="label-left">How do you like our pizza?</span> <!-- First element of the form -->
<div class="checkbox-style inputs-right">
<input id="awesome" type="radio" name="review" value="awesome">
<label for="awesome" class="margin-right">Awesome</label>
<input id="good" type="radio" name="review" value="good">
<label for="good" class="margin-right">Good</label>
<input id="ok" type="radio" name="review" value="ok">
<label for="ok"class="margin-right">Ok</label>
<input id="poor" type="radio" name="review" value="poor">
<label for="poor">Poor</label>
</div>
<span id="error-review" class="label-left error">Please select a response.</span>
</div>
<div class="opinion-radio">
<span class="inline, label-left">What do you think about our prices?</span> <!-- Second element of the form -->
<div class="checkbox-style inputs-right">
<input id="fair" type="radio" name="reviewprice" value="fair">
<label for="fair" class="margin-right">Fair</label>
<input id="okay" type="radio" name="reviewprice" value="okay">
<label for="okay" class="margin-right">Okay</label>
<input id="expensive" type="radio" name="reviewprice" value="expensive">
<label for="expensive">Expensive</label>
</div>
<span id="error-reviewprice" class="label-left error">Please select a response.</span>
</div>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your name:</label> <!-- Third element of the form -->
<input type="text" id="name" name="user_name" class="inline inputs-right">
<span id="error-name" class="error label-left">Please add a name.</span>
</div>
<div class="margin_b">
<label for="name" class=" label-left">Your e-mail address:</label> <!-- Fourth element of the form -->
<input type="email" id="email" name="user_email" class="inline inputs-right">
<span id="error-email" class="label-left error">Please add a correct email.</span>
</div>
<div class="margin_b">
<label for="msg" class="label-left">What can we do better :</label> <!-- Fifth element of the form -->
<textarea id="msg" name="user_message" class="inline inputs-right"></textarea>
<span id="error-text" class="label-left error">Please add a correct text of at least 50 characters.</span>
</div>
<div class="align-right">
<input type="submit" value="Submit" id="btnsubmit">
</div>
</form>


This is my JS (with a first try with the first question of the form):



function formValidation() { 

t1ck=true;

if (!document.getElementById("awesome").checked && !document.getElementById("good").checked && !document.getElementById("ok").checked && !document.getElementById("poor").checked ) {
alert("false");
t1ck=false;
}

if (t1ck == false) {
var btnsubmit = document.getElementById("btnsubmit");
btnsubmit.disabled = true;
btnsubmit.style.backgroundColor = "grey";
} else {
t1ck=true;
alert("true");
btnsubmit.disabled = false;
}
}

function submitCheck() {

var awesome = document.getElementById("awesome");
var good = document.getElementById("good");
var ok = document.getElementById("ok");
var poor = document.getElementById("poor");

t1ck=false;
document.getElementById("btnsubmit").disabled = true;

awesome.onclick = formValidation();
good.onclick = formValidation();
ok.onclick = formValidation();
poor.onclick = formValidation();
}
window.onload = submitCheck;


What I discovered is that alert("false"), which is here for debugging purposes) will always be called when the page load, even if this is linked with an onclick event. Furthermore, I never achieved to trigger alert("true").







javascript forms validation onclick






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 23:15









Ludovic ReneveyLudovic Renevey

65




65













  • You may want to consider reading about HTML5 validation: developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… since browsers now implement most (or maybe all) of what you want natively.

    – Daniel
    Nov 19 '18 at 23:23



















  • You may want to consider reading about HTML5 validation: developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… since browsers now implement most (or maybe all) of what you want natively.

    – Daniel
    Nov 19 '18 at 23:23

















You may want to consider reading about HTML5 validation: developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… since browsers now implement most (or maybe all) of what you want natively.

– Daniel
Nov 19 '18 at 23:23





You may want to consider reading about HTML5 validation: developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… since browsers now implement most (or maybe all) of what you want natively.

– Daniel
Nov 19 '18 at 23:23












1 Answer
1






active

oldest

votes


















0














You define onclick events by calling functions which is wrong.
You need to just use it with function reference without () like that:



awesome.onclick = formValidation;


I guess your button disabling will also work then :)






share|improve this answer


























  • Thanks! This was the correct starting point in order to get it right.

    – Ludovic Renevey
    Nov 20 '18 at 14:54











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383997%2fjavascript-form-validation-disabled-submit-without-any-framework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You define onclick events by calling functions which is wrong.
You need to just use it with function reference without () like that:



awesome.onclick = formValidation;


I guess your button disabling will also work then :)






share|improve this answer


























  • Thanks! This was the correct starting point in order to get it right.

    – Ludovic Renevey
    Nov 20 '18 at 14:54
















0














You define onclick events by calling functions which is wrong.
You need to just use it with function reference without () like that:



awesome.onclick = formValidation;


I guess your button disabling will also work then :)






share|improve this answer


























  • Thanks! This was the correct starting point in order to get it right.

    – Ludovic Renevey
    Nov 20 '18 at 14:54














0












0








0







You define onclick events by calling functions which is wrong.
You need to just use it with function reference without () like that:



awesome.onclick = formValidation;


I guess your button disabling will also work then :)






share|improve this answer















You define onclick events by calling functions which is wrong.
You need to just use it with function reference without () like that:



awesome.onclick = formValidation;


I guess your button disabling will also work then :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 23:44

























answered Nov 19 '18 at 23:28









Roman BatsenkoRoman Batsenko

716




716













  • Thanks! This was the correct starting point in order to get it right.

    – Ludovic Renevey
    Nov 20 '18 at 14:54



















  • Thanks! This was the correct starting point in order to get it right.

    – Ludovic Renevey
    Nov 20 '18 at 14:54

















Thanks! This was the correct starting point in order to get it right.

– Ludovic Renevey
Nov 20 '18 at 14:54





Thanks! This was the correct starting point in order to get it right.

– Ludovic Renevey
Nov 20 '18 at 14:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383997%2fjavascript-form-validation-disabled-submit-without-any-framework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)