关于3-10编程练习

来源:3-13 编程练习

hy_wang

2018-04-26 09:22:45

<!DOCTYPE html>
<html>
    <head>
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<form action="">
<input type="text" id="username" value="" required pattern="^\d{}">
</form>
<script>
var names = document.getElementById("username");
if(names.checkValidity()){
  console.log('符合');
}else{
console.log('不符合');
}
</script>
</body>
</html>

请问老师为什么输入内容控制台就弹出Pattern attribute value ^\d{} is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^\d{}/: Incomplete quantifier错误

写回答

2回答

好帮手慕星星

2018-04-26

第一次粘贴的代码报错是因为正则表达式写错了,{}花括号里面不能没有值,第二次粘贴的代码是可以的哦,继续加油~~

0

hy_wang

提问者

2018-04-26

<!DOCTYPE html>

<html>

    <head>

<meta charset="UTF-8" />

<title></title>

</head>

<body>

<form action="">

<input type="text" id="username" value="" required pattern="^\d{5}" oninput="checkinput()">

</form>

<script>

function checkinput(){

var names = document.getElementById("username");

console.log(names.validity);

if(names.checkValidity()){

  console.log('符合');

}else{

console.log('不符合');

}}

</script>

</body>

</html>

请问老师这样对吗

0

0 学习 · 5012 问题

查看课程