
Untitled
By: a guest on
May 25th, 2012 | syntax:
None | size: 1.39 KB | hits: 29 | expires: Never
How do I write a conditional statement using jquery that validates a page url or an email?
regex for URL "((www.|(http|https|ftp|news|file)+://)[_.a-z0-9-]+.[a-z0-9/_:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])",
regex for email "^[w-.]+@([w-]+.)+[w-]{2,4}$",
error messageelse "You must enter a valid email or you must enter a valid url, which starts with http://. The www prefix is optional.")
var url_regex = /((www.|(http|https|ftp|news|file)+://)[_.a-z0-9-]+.[a-z0-9/_:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])/;
var email_regex = /^[w-.]+@([w-]+.)+[w-]{2,4}$/
$('#test').click(function(){
console.log(url_regex.test($('#test_field').val()));
console.log(email_regex.test($('#test_field').val()));
if(!(url_regex.test($('#test_field').val()) || email_regex.test($('#test_field').val()))){
alert("You must enter a valid email or you must enter a valid url, which starts with http://. The www prefix is optional.")
}
})
var regexU = /((www.|(http|https|ftp|news|file)+://)[_.a-z0-9-]+.[a-z0-9/_:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])/;
var regexEmail = /^[w-.]+@([w-]+.)+[w-]{2,4}$/
function validate() {
if( regexUrl.test("url") || regexEmail.test("email")) {
console.log("valid");
} else {
console.log("You must enter a valid email or you must enter a valid url, which starts with http://. The www prefix is optional.");
}
}