我正在尝试确定粘贴的网址是否有效。我正在使用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>
答案 0 :(得分:7)
你可以试试这个:
$(document).ready(function(){
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);
}
// assign whatever is in the inputbox to a variable
//this is if they paste the url from somewhere
$("#ent").live('input paste', function() {
var url = $("#ent").val();
if(validateURL(url)) {
$("#ent").css("background-color","green");
$("#status").append("Valid URL");
$("#submit").attr('disabled', 'disabled');
}
});
});
答案 1 :(得分:1)
$(document).ready(function(){
//this is if they paste the url from somewhere
$("#ent").bind('paste', function() {
// assign whatever is in the inputbox to a variable
var that = $(this);
if(validateURL(that.val())) {
that.css("background-color","green");
$("#status").append("Valid URL");
$("#submit").attr('disabled', 'disabled');
}
});
});
答案 2 :(得分:1)
用户blur
事件。
<script src="jquery.js"></script>
<script>
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(){
$("#ent").bind('blur',
function ()
{
if(validateURL($(this).val())){
$('#result').html('Valid url')
}
else{
$('#result').html('Invalid url')
}
}
)
});
</script>
<input type="text" name="ent" id="ent">
<input type="submit" name="submit" id="submit">
<label id="result"></label>
<div id="status"></div>
答案 3 :(得分:0)
我正在使用.change方法,因为.live方法不起作用... 按Enter键或选项卡时运行的代码
$(document).ready(function() {
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);
}
$('#myurl').change(function(){
var url = $("#myurl").val();
if(!validateURL(url)) {
$("#status").html("enter Valid URL");
}
else {
$("#status").hide();
}
}); //event handler
}); //document.ready