In the html, you gave the mobilenumber input the name of mobNunber but the javascript looks for mobNumber so I fixed that. Simply checking if arr.length == num to return false during one of these incompleted inputs did not work so I made a variable that is ticked if one of the inputs was not filled in to return false after the for loop ends. I also added arr[num].style.backgroundColor = "transparent"; so if the user fails to fill in an input, the background won’t always stay as that purple-ish color even after filling out the input.
function validate() {
let err = false;
let arr = [];
arr[0] = form.name ;
arr[1] = form.mobNumber ;
arr[2] = form.email ;
arr[3] = form.query ;
for (let num = 0; num < arr.length; num++){
if (arr[num].value == ""){
arr[num].placeholder = "This field is mandatory";
arr[num].style.backgroundColor = "rgba(0, 0, 255, 0.1)";
arr[num].classList.add('redPlaceholder');
err = true;
}
else{
arr[num].style.backgroundColor = "transparent";
}
}
if (err){
return false;
}
console.log(arr[1] + "test")
}
Edit: Instead of making a variable, can simply add event to the function when it is called. onsubmit="return validate(event)" and use e to represnt that when the function actually runs function validate(e) and if the for loop detects that one of the values is equal to zero, you can use the function e.preventDefault() to stop the form from submitting. Example here: https://jsfiddle.net/nukeboy212/vskt8epj/2/.
Solution 2 :
I would utilize preventDefault which will stop the form from submitting, allowing you to run what you need to and then telling the form to submit when you are actually ready.
In the js part, I have an empty array and later add input fields of the form by accessing them through their name.
In the for loop I am checking if each of the input field is filled or not. If not, then I have some code to execute.That code block is running perfectly.
The problem is that when I click submit button with all fields empty, the code in the for loop runs only once – for the first input field. I think that there is some problem with the loop though I have checked the syntax several times.
Comments
Comment posted by John Gordon
There’s a
Comment posted by Praneet Dixit
@JohnGordon I am using return statement so that the form is not submitted (as per my knowledge).
Comment posted by John Gordon
I understand that. You should wait to return false until after you have checked
Comment posted by iAmOren
Add
Comment posted by SirSpeciam
Also if you have any control of the backend here, it would be a good idea to make sure that these inputs are actually filled out as someone can just inspect element and delete the javascript.
Comment posted by SirSpeciam
Another answer I believe is to use
Comment posted by stackoverflow.com/a/4476994/779600
@SirSpeciam Sure, ultimately placement doesn’t really matter. But I feel most will put it at the very beginning of a function as a common practice. Found this:
Comment posted by stackoverflow.com/questions/22363838/…
In this case it does because the person here wants the form to submit normally if the inputs are filled in correctly and if you were to place