-

2011年8月3日星期三

How do you validate a HTML form using JS and display the error message next to the text fields?

-I think I have to use JS. But how can you make the message appear on the side of the text field, right after you type and press submit? If anyone knows how to do this, it would be great. Thanks.You can place a label to the side of each form element, but leave the label blank. When the person clicks Submit and forgot to fill out one of those elements, you can change the label's text.



For example, if you have a label



<label id='label1'></label>



your javascript would be



document.getElementById('label1') . innerHTML = 'Please fill in this field.';



We use innerHTML instead of value because labels don't have a value attribute. If you're going to copy the code, make sure you remove the spaces around the second period.

没有评论:

发表评论