- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题是,当我将 customValidity 设置为输入(#zip_code
)时,每次更新此输入时它都不会停止显示(我在 keyup 事件中使用它并且使用 setCustomValidity('')
无法解决问题)
我有这个html:
<input type="text" required placeholder="Nom" name="lastname" class="col-md-6 form-control" />
<input type="text" required placeholder="Prénom" name="firstname" class="col-md-6 form-control" />
<input type="text" required placeholder="Adresse" name="address" class="col-md-12 form-control" />
<input type="text" required placeholder="Code Postal" name="zip_code" class="col-md-6 form-control" id="zip_code" data-inputmask="'mask': '99999'" />
<input type="text" required placeholder="Ville" name="city" class="col-md-6 form-control disabled" id="city" />
<input type="text" required placeholder="Téléphone" name="phone_number" class="col-md-12 form-control" data-inputmask="'mask': '99 99 99 99 99'" />
<input type="email" required placeholder="Email" name="email" class="col-md-12 form-control" />
还有这个 JavaScript:
var zipCode = "";
$('#zip_code').keyup(function() {
zipLength = zipCode.split('_')[0].length
if (zipLength < 5 || zipLength == 5 && $( this ).val().split('_')[0].length != 5) {
zipCode = $( this ).val();
$('#city').replaceTag('<input>', true);
$('#city').addClass('disabled');
$('#city').focus(function(){$("#zip_code").focus();});
$('#zip_code')[0].setCustomValidity("Veuillez renseigner un code postal valide.");
$('#city').attr('tabindex', -1);
$('#city').attr("placeholder", zipCode.length == 0 ? "Ville" : "Aucune ville pour ce code postal");
$('#city').html('');
if (zipCode.length == 5) {
$.ajax( {
url: "an url to a json representating every city having the zipCode",
dataType: "json",
method: "POST",
data: {
zipCode: zipCode
},
success: function( data ) {
data.cities.map( function( x, i ) {
let cityName = x.name.charAt(0) + x.name.substr(1).toLowerCase().trim();
if (i == 0) {
if (data.cities.length > 1) {
$('#city').removeClass('disabled');
$('#city').replaceTag( '<select>', true );
$("#city").append( new Option( cityName, cityName, true ) );
} else {
$('#city').addClass('disabled');
$('#zip_code')[0].setCustomValidity("Veuillez renseigner un code postal valide.");
$('#city').focus(function(){$("#zip_code").focus();});
$('#city').val( cityName );
}
} else {
$("#city").append( new Option( cityName, cityName ) );
}
} );
}
} );
}
}
});
$(":input").inputmask();
正如你所看到的,我正在使用 inputmask 模块。还有一些完全禁用 #city
输入的技巧(没有直接焦点,没有制表,也没有关注无效性)我必须自己“禁用”,因为“禁用”属性也会禁用“必需”属性。我想我可以通过使用空的 customValidity 以某种方式修补它,但我不知道如何。
最佳答案
像这样的......我想我忘记了一些关闭的if,但这个想法是将验证与keyup分开,并在表单提交时检查它
var zipCode = "";
$("#your-form-id").submit(function(e){
e.preventDefault();
zipLength = zipCode.split('_')[0].length
if (zipLength < 5 || zipLength == 5 && $( this ).val().split('_')[0].length != 5) {
zipCode = $( this ).val();
$('#city').replaceTag('<input>', true);
$('#city').addClass('disabled');
$('#city').focus(function(){$("#zip_code").focus();});
$('#zip_code')[0].setCustomValidity("Veuillez renseigner un code postal
valide.");
$('#city').attr('tabindex', -1);
$('#city').attr("placeholder", zipCode.length == 0 ? "Ville" : "Aucune
ville pour
ce code postal");
$('#city').html('');
return false; // it make the form no submit, if postal code is wrong then the form dont submit
}
});
$('#zip_code').keyup(function() {
if (zipCode.length == 5) {
$.ajax( {
url: "an url to a json representating every city having the zipCode",
dataType: "json",
method: "POST",
`enter code here` data: {
zipCode: zipCode
},
success: function( data ) {
data.cities.map( function( x, i ) {
let cityName = x.name.charAt(0) + x.name.substr(1).toLowerCase().trim();
if (i == 0) {
if (data.cities.length > 1) {
$('#city').removeClass('disabled');
$('#city').replaceTag( '<select>', true );
$("#city").append( new Option( cityName, cityName, true )
);
} else {
$('#city').addClass('disabled');
$('#zip_code')[0].setCustomValidity("Veuillez renseigner
un code postal valide.");
$('#city').focus(function(){$("#zip_code").focus();});
$('#city').val( cityName );
}
} else {
$("#city").append( new Option( cityName, cityName ) );
}
} );
}
} );
}
}
});
$(":input").inputmask();
关于javascript - 我们如何删除 keyup 上的自定义有效性消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54948101/
网站的日常维护通常涉及验证链接是否有效、标记不良链接等。 我知道如何通过脚本匹配电子邮件地址(尤其是在它们位于 mailto: 链接中的网页上下文中)。我的问题是如何在不向地址发送垃圾邮件的情况下验证
我正在尝试将大型 UInt16 数组保存到文件中。 positionCnt大约是50000,stationCnt大约是2500。直接保存,没有GZipStream,文件大约250MB,可以通过外部zi
我正在寻找表格方面的 HTML5 规范。我正在做一个有很多数据表的网络应用程序。 这些表通常有一个数据库行映射到一个表行。一切都很好。然而,一个特定的问题 child 有太多的列,需要将其分布在表中的
我目前正在研究用于客户端目的的Azure流媒体服务,我想知道是否有一种方法可以为自适应流媒体创建时间有限的URL(出于安全目的,我们需要这个,当用户想要流媒体时,我们会验证权限)创建限时 URL)。我
我目前正在研究用于客户端目的的Azure流媒体服务,我想知道是否有一种方法可以为自适应流媒体创建时间有限的URL(出于安全目的,我们需要这个,当用户想要流媒体时,我们会验证权限)创建限时 URL)。我
我想做的是测试我编写的一些 Lucene 代码,并且想要一些有关使用 JUnit 进行测试时的最佳实践的信息。顺便说一句,Lucene 是一个搜索引擎,您可以使用它创建一个平面文件来索引一堆数据。 所
我有一个 .net 核心应用程序,我在注册后创建了一个电子邮件 token ,然后通过电子邮件发送它。这个想法是,用户可以在客户端应用程序中使用该电子邮件 token ,我想检查它的有效性(如果它过期
我的印象是使用 javascript 经常破坏 W3C xhtml/css 有效性。这是真的?如果是这样,为什么我可以在我的网页中使用 javascript 并且仍然使它们符合 W3C 标准(html
我简直是在绞尽脑汁想弄明白这个问题。给定一个无向连通图 G , G 中的所有边都具有未知成本,但已知每条边的每个成本的区间,例如边 e 的成本在闭区间 [i,j] 中,其中 i 和 j 是实数。我还得
检查 URL 有效性的一种直接方法是简单地处理 MalformedURLException 异常: try { URL base = new URL(SomeString); } catch (M
如何在 shell 脚本中检查 0.0.0.0 到 255.255.255.255 范围内的 IP 地址的有效性? 最佳答案 如果您使用的是 bash,则可以对模式进行简单的正则表达式匹配,而无需验证
我已成功通过我的 Angular 应用程序中的 Azure AD ( https://login.microsoftonline.com/ {tenantId}/oauth2/v2.0/authori
我已经获得了文件:一个 .der- 和一个 .p12-文件,以及一个 key 对,我们应该用它来在 Java 代码中创建和验证数字签名。 我必须将两个 key 保存在 MySql-Database-T
我一直在关注 this使用 lumen 和 vuejs 进行身份验证的教程。我正在使用 this库(tymon/jwt-auth)。 一切正常。我的 API 为我的数据处理所有事情,并为我的前端框架提
我想知道,但是引用交易必须在过去 730 天内发生,因为账单协议(protocol) ID 可能在两年后不可用。因此,如果第一笔交易在到期前完成,可以说是在 600 天之后,那么 ID 将在 130
std::string.npos 曾经有效吗? (与正确的 std::string::npos 相反。) 我在我正在处理的一个旧项目中经常看到它,它不能用 VS2010 编译。 它是前标准时代的东西吗
我正在使用 Passport 来验证用户进入我的应用程序 我已经为此创建了一个通行证策略 passport.use(new GoogleStrategy({ clientID: process
我正在使用来自 API 的 URL,其中搜索词用引号括起来。例如:http://www.example.com/searchterm="搜索"。 但是,由于引号,我的 NSURL(由 URLWithS
有谁知道 GitLab 上 OAuth 访问 token 的有效期是多少? 至少需要 12 小时(根据经验),但我想确定一下,所以我不会不必要地刷新 token 。 PS:亚搏体育app ...如果在
使用 FirebaseStorage 的 getDownloadUrl 时,我对 url(&token=) 中的“ token ”有疑问。 https://firebasestorage.google
我是一名优秀的程序员,十分优秀!