开发者

Custom javascript validation

开发者 https://www.devze.com 2023-01-17 12:02 出处:网络
Is there开发者_C百科 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

Is there开发者_C百科 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).

Regards


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号