admin管理员组文章数量:1291148
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\-]+\.(|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\-]+\.(|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>
Share
Improve this question
edited Mar 28, 2011 at 7:56
S L
14.3k18 gold badges79 silver badges119 bronze badges
asked Mar 28, 2011 at 7:47
bob_cobbbob_cobb
2,26911 gold badges52 silver badges115 bronze badges
3
- 1 Just a side ment: You can validate the URL by making a request and looking for a 200 OK return code. More reliable than regex. (If its an existing URL) – Elad Lachmi Commented Mar 28, 2011 at 7:50
- @Elad, I'm unfamiliar with this method. I tried to find something on here about it, but couldn't. api.jquery. is currently down for me as well. – bob_cobb Commented Mar 28, 2011 at 8:19
- You can use jquery.ajax to make a call to the URL and check the status code returned. I would give you a refrence, but jquery. is down, as you said. – Elad Lachmi Commented Mar 28, 2011 at 8:38
4 Answers
Reset to default 7you can try this:
$(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\-]+\.(|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');
}
});
});
jsfiddle
Hmm, couldn't you write it slightly different? I'm not sure if the $("#ent") exists on document ready, that could stop the behaviour.
$(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');
}
});
});
user blur
event instead.
<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\-]+\.(|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>
I am using .change method because .live method not working... code run when you hit enter or tab
$(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\-]+\.(|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
jsfiddle
本文标签: javascriptURL Validation for jQuery paste inputStack Overflow
版权声明:本文标题:javascript - URL Validation for jQuery paste input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515461a2382858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论