I have been learning how to use the console and Firefox's ScratchPad.
I have a form that im writing validation for based on regex. So far it does exactly what i want it to do. The issue is i feel like its too much. Each input requires a different pattern and not all are required.
I attempted to come up with a for loop to handle this but it wasn't giving me the control i needed for each individual input. Is there a way to write for loops for only certain inputs? Or would i have to write one for loop per regular expression
And if what i have is the correct way to do this, is there at least a shorter way of writing it?
Please keep in mind at this point im just testing all the regex as i write them, hence the red stroke green stroke. This is not a validation question. I just want to know a shorter syntax instead of writing each line one by one, because i have about 16 inputs to account for.
// grabs the form
var myForm = document.forms["main-contact"]
// regular expressions
var onlyText = /^[A-Za-zÀ-ÖØ-öø-ÿ]+$/;
var textNumbers = /^[A-Za-zÀ-ÖØ-öø-ÿ0-9\s]+$/;
var onlyEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
// Testing value matches the regular expression
myForm[0].value.match(onlyText) && myForm[0].value.length >= 2 ? myForm[0].setAttribute("style","outline: unset") : myForm[0].setAttribute("style","outline: 2px solid crimson");
myForm[1].value.match(onlyText) && myForm[1].value.length >= 2 ? myForm[1].setAttribute("style","outline: unset") : myForm[1].setAttribute("style","outline: 2px solid crimson");
myForm[2].value.match(onlyEmail) && myForm[2].value.length >= 2 ? myForm[2].setAttribute("style","outline: unset") : myForm[2].setAttribute("style","outline: 2px solid crimson");
myForm[3].value.match(textNumbers) && myForm[3].value.length >= 2 ? myForm[3].setAttribute("style","outline: unset") : myForm[3].setAttribute("style","outline: 2px solid crimson");