Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript form validation, showing error message next to <input>

I'm new to Javascript and HTML and am trying to do some form validation. When the validation() fails for some input like

<input type="text" name="firstName" />

I'd like to have an error message show next to the field that was in error. How do I find the location of where to put the message? Or do I have some sort of label that's hidden to set when there is an error? Thanks.

like image 363
J W Avatar asked Nov 28 '25 23:11

J W


1 Answers

You can have a div tag such as <div id="errorMsg"></div>, and in your javascript, when validation fails, use:

document.getElementById('errorMsg').innerHTML = "error msg";
like image 146
Nick Rolando Avatar answered Dec 01 '25 12:12

Nick Rolando



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!