如何检查屏蔽的输入字段是否为空
How to check if masked input field is empty
我正在使用这个 masked input plugin,我想检查该字段何时为空。以下是我尝试过但似乎不起作用的方法:
HTML
<input type="text" name="phone" id="phoneid" />
勒JavaScript:
$("#phoneid").mask("999-999-9999");
此代码无效
$("#phoneid").keyup(function(){
if($("#phoneid").val().length == 0){
alert(111);
}
});
您使用的屏蔽插件改变了 input
元素的值。
当元素为空时,它的值为___-___-____
。
您可以在检查值的长度时简单地删除 _
/-
个字符:
$("#phoneid").on('keyup', function() {
if (this.value.replace(/[_-]/g, '').length === 0) {
alert('Empty');
}
});
或者,您也可以检查该值是否仅包含 _
/-
个字符:
$("#phoneid").on('keyup', function() {
if (/^[_-]+$/.test(this.value)) {
alert('Empty');
}
});
我们可以通过再次用“9999999999”屏蔽它来获取未屏蔽值的种类,即没有破折号,然后比较如下:
$(document).ready(function(){
$("#phoneid").mask("999-999-9999");
$("#phoneid").keyup(function(){
if($("#phoneid").mask("9999999999").val().length == 0){
alert(111);
}
$("#phoneid").mask("999-999-9999");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/digitalBush/jquery.maskedinput/1.4.1/dist/jquery.maskedinput.min.js"></script>
<input type="text" name="phone" id="phoneid" />
<button id="test">Test</button>
对我来说简单的解决方案。此代码用于检查所有输入,包括屏蔽:
inputs.val() == ""
.on('blur', '.form__input', function(e) {
var inputs = $(this).find('input,textarea,select');
setTimeout(function(){
if (inputs.val().length == 0 || inputs.val() == ""){
alert('Empty');
}
},100);
});
看看我的代码笔:https://codepen.io/ngocquydhtn/pen/YzwVMKR
$(document).ready(function() {
var phoneMask = IMask(
document.getElementById('phoneid'),
{
mask: '0000-000-000',
lazy: false,
placeholderChar: '_'
})
$("#phoneid").keyup(function(){
if(this.value.replace(/[_-]/g, '').length==10){
$(".btn").removeAttr('disabled');
}
else{
$(".btn").attr('disabled','true');
}
})
});
我正在使用这个 masked input plugin,我想检查该字段何时为空。以下是我尝试过但似乎不起作用的方法:
HTML
<input type="text" name="phone" id="phoneid" />
勒JavaScript:
$("#phoneid").mask("999-999-9999");
此代码无效
$("#phoneid").keyup(function(){
if($("#phoneid").val().length == 0){
alert(111);
}
});
您使用的屏蔽插件改变了 input
元素的值。
当元素为空时,它的值为___-___-____
。
您可以在检查值的长度时简单地删除 _
/-
个字符:
$("#phoneid").on('keyup', function() {
if (this.value.replace(/[_-]/g, '').length === 0) {
alert('Empty');
}
});
或者,您也可以检查该值是否仅包含 _
/-
个字符:
$("#phoneid").on('keyup', function() {
if (/^[_-]+$/.test(this.value)) {
alert('Empty');
}
});
我们可以通过再次用“9999999999”屏蔽它来获取未屏蔽值的种类,即没有破折号,然后比较如下:
$(document).ready(function(){
$("#phoneid").mask("999-999-9999");
$("#phoneid").keyup(function(){
if($("#phoneid").mask("9999999999").val().length == 0){
alert(111);
}
$("#phoneid").mask("999-999-9999");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/digitalBush/jquery.maskedinput/1.4.1/dist/jquery.maskedinput.min.js"></script>
<input type="text" name="phone" id="phoneid" />
<button id="test">Test</button>
对我来说简单的解决方案。此代码用于检查所有输入,包括屏蔽: inputs.val() == ""
.on('blur', '.form__input', function(e) {
var inputs = $(this).find('input,textarea,select');
setTimeout(function(){
if (inputs.val().length == 0 || inputs.val() == ""){
alert('Empty');
}
},100);
});
看看我的代码笔:https://codepen.io/ngocquydhtn/pen/YzwVMKR
$(document).ready(function() {
var phoneMask = IMask(
document.getElementById('phoneid'),
{
mask: '0000-000-000',
lazy: false,
placeholderChar: '_'
})
$("#phoneid").keyup(function(){
if(this.value.replace(/[_-]/g, '').length==10){
$(".btn").removeAttr('disabled');
}
else{
$(".btn").attr('disabled','true');
}
})
});