Javascript fallback for the HTML5 “pattern” attribute on <input>

前端 未结 3 1307
余生分开走
余生分开走 2020-12-18 03:12

I\'m using the HTML5 \"pattern\" attribute for client side validation (please don\'t tell me about server-side validation, I have that). I want to use a Javascript or jQuery

3条回答
  •  心在旅途
    2020-12-18 03:50

    On the submit event of your form (or wherever), validate it using its existing pattern property.

    var input = document.getElementsByName('contact[phone]')[0],
        isValid = input.value.search(new RegExp(input.getAttribute('pattern'))) >= 0;
    

    jsFiddle.

    You will want to check browser compatibility first to see if it supports the pattern attribute. You can do that with Modernizr.

提交回复
热议问题