Javascript 提交时的HTML验证-始终返回false
我有一个表单,它包含一系列基于一个下拉列表的字段,下面的输入字段需要更改其验证 例如,我通过以下方式进行设置:Javascript 提交时的HTML验证-始终返回false,javascript,html,validation,Javascript,Html,Validation,我有一个表单,它包含一系列基于一个下拉列表的字段,下面的输入字段需要更改其验证 例如,我通过以下方式进行设置: $("#Identifier").attr("pattern", "[0-9]{11}"); $("#Identifier").attr("oninvalid", "setCustomValidity('Must be 11 digits.')"); 这反过来又表明: 但是,无论何时输入该字段,它总是显示该字段的错误 我不知道如何获得积极的结果,也不知道我错过了什么 编辑: 以下是
$("#Identifier").attr("pattern", "[0-9]{11}");
$("#Identifier").attr("oninvalid", "setCustomValidity('Must be 11 digits.')");
这反过来又表明:
但是,无论何时输入该字段,它总是显示该字段的错误
我不知道如何获得积极的结果,也不知道我错过了什么
编辑:
以下是我的完整代码:
<body>
<form id="form1" runat="server">
<div id="main-bg">
<table width="960" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" class="style67">
<div id="main"><div class="style9" style="width: 701px">
<!-- Main //-->
<br />
<br />
<span class="auto-style1">
<div id="main-title" class="style54" style="width: 475px">
Choose your primary country of nationality</div></span>
<br class="auto-style1" />
<select id = "CountrySelect" required>
<option value="">Choose your country</option>
<option value="Austria">Austria</option>
<option value="Belgium">Belgium</option>
<option value="Bulgaria">Bulgaria</option>
<option>Croatia</option>
<option>All other countries</option> </select>
<br />
<br />
<span class="auto-style1">Select your National Identifier type:</span><br class="auto-style1" />
<select id = "IdentifierSelect" required class="auto-style2">
</select>
<span class="auto-style1">
<br />
<br />
Enter the value</span><br/>
<input type="text" pattern="" name="Identifier" id="Identifier" required><br />
<span class="auto-style1"><strong>
<br />
</strong></span>
<div id="main-title" class="style54" style="width: 475px">
<!-- Title //-->
<% IF strWebCountry = "" Then
Response.Write("<input name=Submit type=submit value=""Maturity Instructions"" /></form></p>")
else
Response.Write("")
end if
%>
</div>
</div> </td>
</tr>
</table>
<div id="spacer"></div>
</form>
</body>
</html>
<script type="text/javascript">
$('#CountrySelect').on('change', function(){
console.log($('#CountrySelect').val());
$('#IdentifierSelect').html('');
if($('#CountrySelect').val()=="Austria"){
$('#IdentifierSelect').append('<option value="CONCAT">CONCAT</option>');
$("#Identifier").attr("pattern", "[0-9A-Z]{8}");
}
else if($('#CountrySelect').val()=="Belgium"){
$('#IdentifierSelect').append('<option value="Belgian National Number">Belgian National Number</option>');
$('#IdentifierSelect').append('<option value="CONCAT">CONCAT</option>');
}
else if($('#CountrySelect').val()=="Bulgaria"){
$('#IdentifierSelect').append('<option value="Bulgarian Personal Number">Bulgarian Personal Number</option>');
$('#IdentifierSelect').append('<option value="CONCAT">CONCAT</option>');
}
else{
$('#IdentifierSelect').append('<option value="National Passport Number">National Passport Number</option>');
$('#IdentifierSelect').append('<option value="CONCAT">CONCAT</option>');
}
});
</script>
<script type="text/javascript">
$('#Identifier').on('focus', function(){
console.log($('#IdentifierSelect').val());
$('#IdentifierSelect12').html('');
if($('#IdentifierSelect').val()=="Belgian National Number"){
$("#Identifier").attr("pattern", "[0-9]{11}");
$("#Identifier").attr("oninvalid", "setCustomValidity('Must be 11 digits.')");
$("#Identifier").attr("required");
}
});
</script>
选择你的主要国籍国
选择您的国家标识类型:
输入值
$('#CountrySelect')。在('change',function()上{
log($('#CountrySelect').val());
$('#IdentifierSelect').html('');
如果($('#CountrySelect').val()=“奥地利”){
$('#IdentifierSelect')。追加('CONCAT');
$(“#标识符”).attr(“模式”,“[0-9A-Z]{8}”);
}
else if($('#CountrySelect').val()=“比利时”){
$(“#IdentifierSelect”)。附加(‘比利时国家编号’);
$('#IdentifierSelect')。追加('CONCAT');
}
else if($('#CountrySelect').val()=“保加利亚”){
$('#IdentifierSelect')。追加('保加利亚个人号码');
$('#IdentifierSelect')。追加('CONCAT');
}
否则{
$('#IdentifierSelect')。附加('National Passport Number');
$('#IdentifierSelect')。追加('CONCAT');
}
});
$('#Identifier')。在('focus',function()上{
console.log($('#IdentifierSelect').val());
$('#IdentifierSelect12').html('');
if($('#IdentifierSelect').val()=“比利时国家编号”){
$(“#标识符”).attr(“模式”,“[0-9]{11}”);
$(“#标识符”).attr(“oninvalid”、“setCustomValidity('必须是11位)。”);
$(“#标识符”).attr(“必需”);
}
});
我无法重现问题oninvalid的输入对我来说很好。。。你用的是IE 9还是Safari?Internet Explorer 9及更早版本或Safari中不支持oninvalid事件。页面中必须有其他内容,我将把它剥离到最基本的部分,然后添加到其中。以下是完整代码:@Quentin您有工作示例吗?