正则表达式始终为false



所以我正在编写一些javascript来检查用户是否输入了gmail地址。

目前我有下面的代码,但无论我做什么,它都会给我false。即使输入有@gmail.com……我觉得错误在于我如何获取变量中的用户输入,然后用test()进行测试。

Javascript:

<script>
function validationCheck() {
var email = document.getElementById("inputEmail");
var gmailPatt = /@gmail.com/i;
var emailMatch = gmailPatt.test(email);
if (emailMatch == false || emailMatch == true) {
document.getElementById("emailError").innerHTML =
"<p>" + emailMatch + "</p>";
} else {
}
}
</script>

html

<form>
<div class="form-group row">
<div>
<label for="inputEmail" class="col-sm-1">Email:</label>
<input
type="text"
class="form-control col-sm-1"
id="inputEmail"
placeholder="username@gmail.com"
/>
<p id="emailError"></p>
</div>
</div>
<button type="button" onclick="validationCheck()">Check</button>
</form>

您需要检查输入的值var emailMatch = gmailPatt.test(email.value);

function validationCheck() {
var email = document.getElementById("inputEmail");
var gmailPatt = /@gmail.com/i;
var emailMatch = gmailPatt.test(email.value);
if (emailMatch == false || emailMatch == true) {
document.getElementById("emailError").innerHTML =
"<p>" + emailMatch + "</p>";
} else {}
}
<form>
<div class="form-group row">
<div>
<label for="inputEmail" class="col-sm-1">Email:</label>
<input type="text" class="form-control col-sm-1" id="inputEmail" placeholder="username@gmail.com" />
<p id="emailError"></p>
</div>
</div>
<button type="button" onclick="validationCheck()">Check</button>
</form>

相关内容

  • 没有找到相关文章

最新更新