I've copied the Client-Side form validation from MDN and editied it a little bit.
When I type anything into the input field I get this errors...
Uncaught TypeError: Cannot set property 'textContent' of null at showError (validation.js:74) at HTMLInputElement.lnl (validation.js:37)
Uncaught TypeError: Cannot set property 'textContent' of null at showError (validation.js:104) at HTMLInputElement.fnl (validation.js:23)
Uncaught TypeError: Cannot set property 'textContent' of null at showError (validation.js:108) at HTMLInputElement.eml (validation.js:48)
Uncaught TypeError: Cannot set property 'textContent' of null at showError (validation.js:108) at HTMLInputElement.check (validation.js:63)
I'm sure there is a simple solution, but I can't seem to figure it out (I've read through a bunch of questions here on SO, but I can't seem to find a solution.).
function check (event) {
// if the email field is valid, we let the form submit
if(!fname.validity.valid || !lname.validity.valid || !email.validity.valid) {
// If it isn't, we display an appropriate error message
showError();
// Then we prevent the form from being sent by canceling the event
event.preventDefault();
}
};
document.getElementById('submit_ok').addEventListener('click', check);
function showError() {
if(fname.validity.valueMissing) {
// If the field is empty
// display the following error message.
fnameError.textContent = 'You need to enter your name.';
} else if(fname.validity.typeMismatch) {
// If the field doesn't contain an email address
// display the following error message.
fnameError.textContent = 'Name should contain only letters.';
} else if(fname.validity.tooShort) {
// If the data is too short
// display the following error message.
fnameError.textContent = 'Name must contain at least 2 characters';
} else if (fname.validity.tooLong) {
fnameError.textContent = 'Name should contain no more then 45 characters.';
}
<form accept-charset="UTF-8" name="form" onsubmit="return false;" method="post" id="form">
<input type="text" oninvalid="This field may contain only letters from A to z." pattern="[A-Za-z][^0-9]{1,25}"
name="fname" id="fname" required maxlength="50" minlength="1" placeholder="Janez"/><br />
<span class="error fn" aria-live="polite"></span>
<input type="text" oninvalid="This field may contain only letters from A to z." pattern="[A-Za-z][^0-9]{1,25}"
name="lname" id="lname" required maxlength="50" minlength="1" placeholder="Novak"/><br />
<span class="error ln" aria-live="polite"></span>
<span class="lng sl radioS">Spol: </span>
<span class="lng en radioS">Gender: </span>
<span class="lng de radioS">Spol DE: </span>
<span class="lng it radioS">Gendero IT: </span>
<span class="lng hr radioS">Spolov HR: </span>
<span class="lng ru radioS">Spol RU: </span><br /><br />
<div class="radioC"><label><input type="radio" name="spol" id="spM" value="M"> <span class="lng sl">Moški SI</span>
<span class="lng en">Moški EN</span>
<span class="lng de">Moški DE</span>
<span class="lng it">Moški IT</span>
<span class="lng hr">Moški HR</span>
<span class="lng ru">Moški RU</span>
</label></div><br />
<div class="radioC"><label><input type="radio" name="spol" id="spZ" value="Z"> <span class="lng sl">Ženski SI</span>
<span class="lng en">Ženski EN</span>
<span class="lng de">Ženski DE</span>
<span class="lng it">Ženski IT</span>
<span class="lng hr">Ženski HR</span>
<span class="lng ru">Ženski RU</span>
</label></div><br />
<div class="radioC"><label><input type="radio" name="spol" id="spO" value="O"> <span class="lng sl">Ostalo SI</span>
<span class="lng en">Ostalo EN</span>
<span class="lng de">Ostalo DE</span>
<span class="lng it">Ostalo IT</span>
<span class="lng hr">Ostalo HR</span>
<span class="lng ru">Ostalo RU</span>
</label></div><br /><br />
<input type="email" name="email" id="email" autofocus="autofocus" autocorrect="off"
autocapitalize="off" maxlength="45" required placeholder="moj@email.si"/>
<br />
<span class="error em" aria-live="polite"></span>
<input type="button" value="Submit" id="submit_ok" name="submit_ok" /> <br />
The live preview can be found here, the JS file with the errors is called validate.js.
Thank you for the help