我是编码的新手,我确信您可以从我的问题中得知,因为我觉得这应该很容易完成,但是我为此付出的努力已经超过了我愿意承认的时间,现在必须寻求指导。
在下面的代码中,无论输入的内容是否为有效的邮政编码,我都会收到警报“不是邮政编码”。
$("body").on("click", "#searchBtn", function (event) {
event.preventDefault();
// The below is a regular expression (regex) to ensure user enters either a 5 digit or 9 digit US zip code format
var zip = ("^[0-9]{5}(?:-[0-9]{4})?$;");
var input = $("#userInput").val().trim();
if (input!=zip) {
alert("Not a Zip Code"); //the end goal is to have the placeholder color turn red
} else {
alert("Thank you - your entry is valid"); //the end goal is to have the placeholder say "You are searching in zip code " (+ zip)"
};
});
在此问题上-带-当我更换时:alert(“非邮政编码”);使用(目前,我已经尝试了多种格式,但是一个示例是:
$("#userInput").addClass('red');
对于上述内容,我在CSS中添加了以下内容:
.red::placeholder {
color: red;
}
我也在此公告板上搜索了类似的问题,但是它们比我目前的理解要先进,或使用我不熟悉的程序。预先感谢您的协助!
.toggleClass()
method.toggleClass()
^\d{5}(-\d{4})?$
$("body").on("click", "#searchBtn", function(event) {
event.preventDefault();
var $input = $("#userInput");
var input = $input.val().trim();
// Ensure user enters either a 5 digit or 9 digit US zip code format
var isValid = /^\d{5}(-\d{4})?$/.test(input);
$input.toggleClass('is-invalid', !isValid);
alert(isValid ? "Thank you - your entry is valid" : "Not a Zip Code");
});
.is-invalid::placeholder { /* Don't use color-specific classes! */
color: red;
}