现象:
今天找到了一个网上反馈比较好的用于数据类型验证的库:“validator.js”,想用测试html检验一下效果,于是就写了如下代码:
<html> <head> <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="validator.min.js"></script> </head> <body> <script type="text/javascript"> window.alert(validator.isEmail(‘foo@bar.com‘)); //=> true </script> <div style="height:500px; background-color:gray;"> <div style="position:relative; top:50%;left:50%; transform:translateY(-50%) translateX(-50%); background-color:blue; width:200px; height:100px;"> </div> </div> </body> </html>
运行之后报错,错误信息如下:
validator.min.js:23 Uncaught SyntaxError: Invalid regular expression: /^[?‘-?‰]+$/: Range out of order in character class at validator.min.js:23 at validator.min.js:23 at validator.min.js:23 testDivMiddle.html:10 Uncaught ReferenceError: validator is not defined at testDivMiddle.html:10
解决方法:
因为怀是字符集的问题,所以添加了一行如下代码:
<meta charset="utf-8" />
结果就正常运行了。
全部代码如下:
<html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="validator.min.js"></script> </head> <body> <script type="text/javascript"> window.alert(validator.isEmail(‘foo@bar.com‘)); //=> true </script> <div style="height:500px; background-color:gray;"> <div style="position:relative; top:50%;left:50%; transform:translateY(-50%) translateX(-50%); background-color:blue; width:200px; height:100px;"> </div> </div> </body> </html>