内联主动 Javascript 表单验证
我正在处理联系表单,需要一些内联 javascript 表单验证。我已经有 php 验证,但我想进行一些活动表单验证,例如 http://twitter.com/signup。我希望它在输入后显示和隐藏 p
标签。这是我的 html 代码。
<form class="contact" method="POST" enctype="multipart/form-data" action="validate.php">
<label for="fname">First Name*<br /></label>
<input type="text" id="fname" style="font-family: Gochi Hand;" name="fname" placeholder="First" autofocus required autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">First Name Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">A First Name is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your First Name.</p>
</div>
<br /><br />
<label for="lname">Last Name*<br /></label>
<input type="text" id="lname" style="font-family: Gochi Hand;" name="lname" placeholder="Last" required autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice" style="color:green; ">Last Name Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">A Last Name is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your Last Name.</p>
</div>
<br /><br />
<label for="email">Email Address*<br /></label>
<input type="email" name="email" style="font-family: Gochi Hand;" id="email" placeholder="[email protected]" pattern="^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$" required autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">Email Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">A Email is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your Email.</p>
</div>
<br /><br />
<label for="url">Website<br /></label>
<input type="url" name="url" id="url" style="font-family: Gochi Hand;" placeholder="http://website.com" pattern="^(http|https)://.+(.[a-zA-Z])$" autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">URL Looks Good.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your URL.</p>
</div>
<br /><br />
<label for="age">Age*<br /></label>
<input type="text" size="3" id="age" name="age" style="font-family: Gochi Hand;" required class="age" required placeholder="Age" pattern="^\d+$" autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">Age Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">An Age is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your Age.</p>
</div>
<br /><br />
<label for="comments">Comments*<br /></label>
<textarea style="font-family: Gochi Hand;" required id="comments" name="comments" cols="30" rows="10"></textarea>
<br /><br />
<input type="hidden" value="True" name="go" id="go" />
<input style="color:red;" type="submit" class="submit" value="Send!" />
</form>
任何建议或帮助都非常有价值。
I am working on a contact form and need some inline javascript form validation. I already have php validation, but I would like to have some active form validation like at http://twitter.com/signup. I would like it to show and hide the p
tags after the input. Here is my html code.
<form class="contact" method="POST" enctype="multipart/form-data" action="validate.php">
<label for="fname">First Name*<br /></label>
<input type="text" id="fname" style="font-family: Gochi Hand;" name="fname" placeholder="First" autofocus required autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">First Name Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">A First Name is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your First Name.</p>
</div>
<br /><br />
<label for="lname">Last Name*<br /></label>
<input type="text" id="lname" style="font-family: Gochi Hand;" name="lname" placeholder="Last" required autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice" style="color:green; ">Last Name Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">A Last Name is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your Last Name.</p>
</div>
<br /><br />
<label for="email">Email Address*<br /></label>
<input type="email" name="email" style="font-family: Gochi Hand;" id="email" placeholder="[email protected]" pattern="^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$" required autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">Email Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">A Email is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your Email.</p>
</div>
<br /><br />
<label for="url">Website<br /></label>
<input type="url" name="url" id="url" style="font-family: Gochi Hand;" placeholder="http://website.com" pattern="^(http|https)://.+(.[a-zA-Z])$" autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">URL Looks Good.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your URL.</p>
</div>
<br /><br />
<label for="age">Age*<br /></label>
<input type="text" size="3" id="age" name="age" style="font-family: Gochi Hand;" required class="age" required placeholder="Age" pattern="^\d+$" autocomplete="on" />
<div class="notices">
<p id="helper" style="color:green;" class="g-notice">Age Looks Good.</p>
<p id="helper" style="color:red;" class="r-notice">An Age is required.</p>
<p id="helper" style="color:#0099FF;" class="h-notice">Enter Your Age.</p>
</div>
<br /><br />
<label for="comments">Comments*<br /></label>
<textarea style="font-family: Gochi Hand;" required id="comments" name="comments" cols="30" rows="10"></textarea>
<br /><br />
<input type="hidden" value="True" name="go" id="go" />
<input style="color:red;" type="submit" class="submit" value="Send!" />
</form>
any suggestions or help would be worth a ton.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个示例表单
html
css
javascript
这是一个非常简单的示例,有不同的方法。例如,如果出现错误,您可以只附加一个元素,而不是隐藏一个元素。此外,您还可以添加另一个函数,以在
input
元素上存在onblur
事件时检查有效值。Here is a sample form
html
css
javascript
This is a very simple example and there are different ways to it. For example you could just append an element if there is an error instead of having one hid. Also, you can add another function to check for valid value when there is an
onblur
event on theinput
elements.一般来说,您希望创建一个事件处理程序,当用户与输入字段交互时调用该事件处理程序并评估输入值以确定要显示的消息。显示适当的消息并隐藏所有其他消息。您可以在浏览器中执行简单的模式匹配,或使用 AJAX 将输入发送到服务器以评估更复杂的验证,例如电子邮件是否已在使用中。
Twitter 注册表单使用事件组合来触发评估脚本。貌似onclick(或者onfocus)、onchange、onblur都用了。单击时显示说明(相当于“.h-notice”),并根据模糊(元素失去焦点)评估输入。
我会使用 jQuery,因为您需要复杂的元素选择和事件处理。您可以编写简单的 javascript 来实现相同的效果,但这需要更多的代码。名字字段的 jQuery 代码看起来像这样:
BTW:HTML 元素 ID 应该是唯一的。您可以将“helper”设置为一个类或使每个 id 不同(例如“fname-helper-h”)。
In general, you want to make an event handler that is called when the user interacts with the input fields and evaluates the input value to determine which message to show. The appropriate message is displayed and all others are hidden. You can execute simple pattern matching in the browser or use AJAX to send input to the server to evaluate more complex validation, such as if an email is already in use.
The twitter signup form uses a combination of events to trigger the evaluation script. It looks like onclick(or onfocus), onchange, and onblur are all used. The instructions are displayed on click (your ".h-notice" equivalent) and the input is evaluated on blur (element loses focus).
I would use jQuery because you want complex element selection and event handling. You could write plain javascript to accomplish the same effect, but it would require a lot more code. jQuery code for the first name field would look something like this:
BTW: HTML element IDs should be unique. You can make "helper" a class or make each id different (e.g. "fname-helper-h").