简体   繁体   English

用于HTML5表单字段验证的合法正则表达式

[英]Legitimate regex for HTML5 form field validation

Is the following regex: 是以下正则表达式:

  1. url: /\\b(http|https|ftp):\\/\\/([-A-Z0-9.]+)(\\/[-A-Z0-9+&@#\\/%=~_|!:,.;]*)?(\\?[A-Z0-9+&@#\\/%=~_|!:,.;]*)?/i 网址: /\\b(http|https|ftp):\\/\\/([-A-Z0-9.]+)(\\/[-A-Z0-9+&@#\\/%=~_|!:,.;]*)?(\\?[A-Z0-9+&@#\\/%=~_|!:,.;]*)?/i
  2. email: /\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[AZ]{2,6}\\b/i 电子邮件:/ /\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[AZ]{2,6}\\b/i :[ /\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[AZ]{2,6}\\b/i

I am using them in inputs like: 我在输入中使用它们,例如:

  <section id='email-txt' class='flex-column'>
    <label id='address' for='emailTxt'>Address</label>
    <input id=emailTxt
           type='email'
           value='{{webContact.homeEmail}}'
           pattern=/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b/i>

  </section>


  <section id='col2' class='flex-column'>
    <label id='type' for='webPageUrl'>Type</label>
    <input id=webPageUrl
           type='url'
           value='{{webContact.homeEmail}}'
           placeholder='http://microsoft.com'
           required
           pattern=/\b(http|https|ftp):\/\/([-A-Z0-9.]+)(\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#\/%=~_|!:,.;]*)?/i>
  </section>

in forms but the validation is always incorrect. 形式,但验证始终不正确。

Thanks 谢谢

The word boundaries \\b are not pertinent in your case. 边界\\b一词与您的情况无关。 If you do input fields validation, try those regexes with ^ and $ : 如果您进行输入字段验证,请尝试使用^$表达式:

url: 网址:

/^(http|https|ftp):\/\/([-A-Z0-9.]+)(\/[-A-Z0-9+&@#\/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#\/%=~_|!:,.;]*)?$/i

email: 电子邮件:

/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM