HTML 输入模式验证不起作用

标签 html forms validation

我正在尝试使用 pattern 属性验证我的 HTML 输入。在某些情况下,它可以正常工作,而在某些情况下,它无法正常工作,而且我无法弄清楚它为什么会崩溃。

给大家分享两段代码。代码 1 正在运行。代码 2 不是。我先写了代码 1,然后写了代码 2。我从字面上复制粘贴了代码 2,但出于某种我无法理解的原因,它不起作用。检查一下。

CODE1(正在工作的那个)

<input id="clinicProfileNumber" name="clinicProfileNumber" ng-model="clinic.profile.number" type="text" class="user-input" pattern="[0-9]{10}" title="10 digit mobile number">

Code2(不工作的那个)

<input id="doctorProfileContactnumber" name="doctorProfileContactnumber" ng-model="doctor.profile.contactNumber" type="text" class="user-input" pattern="[0-9]{10}" title="10 digit mobile number">

让我给你一些其他不工作的代码示例:

<input id="doctorProfileEmail"
       name="doctorProfileEmail"
       ng-model="doctor.profile.email"
       pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$"
       required type="text"
       class="user-input"
       title="Please enter a valid email." />

<input id="doctorProfilePassword"
       name="doctorProfilePassword"
       ng-model="doctor.profile.password"
       type="text"
       class="user-input"
       pattern=".{6,}"
       title="Must contain at least 6 or more characters">

</div>

请检查这个,让我知道我做错了什么。

最佳答案

您的代码看起来很棒。只需将它添加到表单标签中,就像这样

<form action="demo">
    Country code: <input type="text"
                         name="country_code"
                         pattern="[A-Za-z]{6,}"
                         title="Three letter country code">

    <input id="doctorProfileContactnumber"
           name="doctorProfileContactnumber"
           ng-model="doctor.profile.contactNumber"
           type="text"
           class="user-input"
           pattern="[0-9]{10}"
           title="10 digit mobile number">

    <input type="submit">
</form>

关于HTML 输入模式验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34783467/

相关文章:

java - 将计算结果传递到JSP页面

php - 如何解决 HTTP/1.1 302 Found error when trying to get a contents of a form in php?

php - 删除 PHP 中的换行符 - 但它们仍显示在 MySQL 的 LONGTEXT 中

validation - 如何向输入字段添加验证?

html - 如何使用 html/css 使表格只有一行高?

html - 使用 BEM 和 Less 时如何在子元素上应用悬停选择器?

javascript - 如何让付款直接通过我的网站(充当代理)流向实际卖家?

html - 为什么我的 CSS `ul li ~ li` 没有调整我的 HTML 元素之一?

html - 隐藏侧边栏时,鼠标悬停时菜单文本仍然可见

ruby-on-rails - Rails-仅当另一个字段具有特定值时,才如何验证一个字段?