最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript form validation, showing error message next to <input> - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Oct 26, 2011 at 0:47 J WJ W 8781 gold badge12 silver badges25 bronze badges 1
  • 1 Why not use jQuery validation plugin? – Emmanuel N Commented Oct 26, 2011 at 0:52
Add a ment  | 

1 Answer 1

Reset to default 3

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";
发布评论

评论列表(0)

  1. 暂无评论