jQuery匹配模式
问题描述:
我在尝试在发送表单之前用jQuery 验证我的输入。
I'm trying to validate my inputs with jQuery before a form is sent.
这是我的输入字段在数据库中插入无值:
This is what my input fields look like when there is no value inserted in the database:
<input
class="field"
type="text"
style="background-color: rgb(255, 255, 255);"
value="" name="input_18748_388_1257894000_"/>
这是我的输入字段看起来像有现有值插入数据库:
This is what my input fields look like when there is an existing value inserted in the database:
<input
class="field"
type="text"
style="background-color: rgb(255, 255, 255);"
value=""
name="input_18748_388_1257894000_46549876"/>
我想:
I would like to:
-
检查该值是否已在数据库中
Check if the value is already in the database
用户想要将
现有值替换为零或零和
禁止它。
Check if the user want to replace an existing value by nothing or zero and disallow it.
检查用户是否尝试
在新字段中插入0,
不允许。
Check if the user is trying to insert 0 in a new field and disallow it.
已解决:
Solved:
$("input[name^='input_"+var+"_']")
.each(function() {
if ($(this).attr('name').match(/^input_\d+_\d+_\d+_\d+/)
&& ($(this).val() == '' || $(this).val() <= 0))
{
displayDialog("<?=_('error')?>")
flag_error = 1;
return false;
}
});
// Submit the form.
答
$("input[name^='input_"+var+"_']")
.each(function() {
if ($(this).attr('name').match(/^input_\d+_\d+_\d+_\d+/)
&& ($(this).val() == '' || $(this).val() <= 0))
{
displayDialog("<?=_('error')?>")
flag_error = 1;
return false;
}
});