jQuery 粘贴输入的 URL 验证
我正在尝试确定粘贴的网址是否有效。我正在使用 bind() 函数来检测粘贴事件。我正在使用我在此处找到的正则表达式进行验证(该正则表达式有效并且暂时没问题)。我还附加了一些文本来告诉用户它是否是有效的网址。
当它在 bind() 内部时,唯一不起作用的是 URL 验证。不过,当放置在其外部时它会起作用。
JS:
function validateURL(textval) {
var urlregex = new RegExp( "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test(textval);
}
$(document).ready(function(){
// assign whatever is in the inputbox to a variable
var url = $("#ent").val()
//this is if they paste the url from somewhere
$("#ent").bind('paste', function() {
if(validateURL(url)) {
$("#ent").css("background-color","green");
$("#status").append("Valid URL");
$("#submit").attr('disabled', 'disabled');
}
});
});
HTML:
<input type="text" name="ent" id="ent">
<input type="submit" name="submit" id="submit">
<div id="status"></div>
I'm trying to determine if a pasted URL is valid. I'm using the bind() function to detect a paste event. I'm using a regex that I found on here for the validation (which works and is fine for the time being). I'm also appending some text to tell the user if it's a valid url or not.
The only thing that isn't working when it's inside the bind() is the URL validation. It works when placed outside of it, though.
JS:
function validateURL(textval) {
var urlregex = new RegExp( "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test(textval);
}
$(document).ready(function(){
// assign whatever is in the inputbox to a variable
var url = $("#ent").val()
//this is if they paste the url from somewhere
$("#ent").bind('paste', function() {
if(validateURL(url)) {
$("#ent").css("background-color","green");
$("#status").append("Valid URL");
$("#submit").attr('disabled', 'disabled');
}
});
});
HTML:
<input type="text" name="ent" id="ent">
<input type="submit" name="submit" id="submit">
<div id="status"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以试试这个:
jsfiddle
you can try this:
jsfiddle
嗯,你就不能写得稍微不同一点吗?我不确定 $("#ent") 是否存在于准备好的文档中,这可能会阻止该行为。
Hmm, couldn't you write it slightly different? I'm not sure if the $("#ent") exists on document ready, that could stop the behaviour.
用户
blur
事件代替。user
blur
event instead.我正在使用 .change 方法,因为 .live 方法不起作用......
当您按 Enter 或 Tab
jsfiddle时运行代码
I am using .change method because .live method not working...
code run when you hit enter or tab
jsfiddle