我正在尝试使用 JS 验证字段输入的正则表达式模式。
我的代码是:
<script>
function expiry_check(){
var y;
y = document.getElementById("expiry_date").value;
var x;
x = /(0[1-9]|10|11|12)/(18|19|20|21|22|23|24|25|26)/;
if (y !== "") {
if (x.test(y)) {
}
else {
document.getElementById('expiry_date').value = '';
document.getElementById('expiry_date').style = 'border-color:red';
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
HTML 字段:
<input type="text" name="expiry_date" id="expiry_date" value="" onkeypress='return event.charCode >= 48 && event.charCode <= 57' onblur="expiry_check()">
Run Code Online (Sandbox Code Playgroud)
由于某种原因,如果将正则表达式用作“模式”属性,则它可以工作,但我需要使用 Javascript 来实现,并且当指定为 JS 代码时它不起作用。
我究竟做错了什么?我已经使用不同的正则表达式字符串测试了相同的函数,并且它工作正常 - 所以问题在于正则表达式本身。
我一直在从事一个项目,该项目要求我在表单密码字段上实现一个显示/隐藏按钮,该按钮在将密码显示为纯文本和将其隐藏在星号后面之间切换。
到目前为止我想出了什么:
function pass(){ document.getElementById('password').type="password"; }
function text(){ document.getElementById('password').type="text"; }Run Code Online (Sandbox Code Playgroud)
<input type="password" id="password" />
<button class="ui-component__password-field__show-hide" type="button" onclick="text()">Show</button>Run Code Online (Sandbox Code Playgroud)
它可以很好地切换到显示密码,但是如何在显示密码后将按钮的文本更改为“隐藏”,并使其执行 pass() 函数?