自定义JavaScript验证 [英] Custom javascript validation

查看:178
本文介绍了自定义JavaScript验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过JavaScript显示自定义输入验证消息?我有Form.Submit innvoked JavaScript验证方法,因此,如果验证出错,我想有一个自定义文本字段旁边我已经验证(类似ASP.NET MVC 2验证摘要)。

Is there any way to show custom input validation message through javascript? I have javascript validation method innvoked with Form.Submit, so if validation goes wrong, I'd like to have a custom text next to the field I have validated (similar as ASP.NET MVC 2 validation summary).

问候

推荐答案

是的,可以。只要创建一个<跨度方式> 各个领域后,执行JavaScript中的验证,以及验证失败时的错误信息填充此字段

Yes, you can. Just create a <span> after every field, do the validation in javascript, and populate this field with the error message when validation fails.

<script>
   function validate()
   {
      if(document.getElementById('txtName').value="")
      {
         document.getElementById('errName').innerHTML = 'Please enter your name';
      }
   }
</script>

<form onsubmit=validate()>
   <input type="text" id="txtName" name="txtName"/>
   <span name="errName" class="error" />
</form>

这不是你在看什么?

Isn't this what you are looking at ?

这篇关于自定义JavaScript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆