Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 1.39 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I write a conditional statement using jquery that validates a page url or an email?
  2. regex for URL "((www.|(http|https|ftp|news|file)+://)[&#95;.a-z0-9-]+.[a-z0-9/&#95;:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])",
  3.  
  4.  
  5. regex for email "^[w-.]+@([w-]+.)+[w-]{2,4}$",
  6.  
  7.  error messageelse "You must enter a valid email or you must enter a valid url, which starts with http://. The www prefix is optional.")
  8.        
  9. var url_regex = /((www.|(http|https|ftp|news|file)+://)[&#95;.a-z0-9-]+.[a-z0-9/&#95;:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])/;
  10. var email_regex = /^[w-.]+@([w-]+.)+[w-]{2,4}$/
  11.  
  12. $('#test').click(function(){
  13.     console.log(url_regex.test($('#test_field').val()));
  14.     console.log(email_regex.test($('#test_field').val()));
  15.  
  16.     if(!(url_regex.test($('#test_field').val()) || email_regex.test($('#test_field').val()))){
  17.         alert("You must enter a valid email or you must enter a valid url, which starts with http://. The www prefix is optional.")
  18.     }
  19. })
  20.        
  21. var regexU = /((www.|(http|https|ftp|news|file)+://)[&#95;.a-z0-9-]+.[a-z0-9/&#95;:@=.+?,##%&~-]*[^.|'|# |!|(|?|,| |>|<|;|)])/;
  22. var regexEmail = /^[w-.]+@([w-]+.)+[w-]{2,4}$/
  23.  
  24. function validate() {
  25.     if( regexUrl.test("url") || regexEmail.test("email")) {
  26.     console.log("valid");
  27.     } else {
  28.       console.log("You must enter a valid email or you must enter a valid url, which starts with http://. The www prefix is optional.");
  29.     }
  30. }