我正在尝试使用 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/