1,105,380 Community Members

Set condition for checking url using jQuery validation

Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

Sorry if the title of this thread doesn't make sense - I didn't know what to write :)

I am using jQuery form validation and one of the rules is to check for a valid url. The field isn't required so am just validating formatting. This all works fine but where the complication comes (for me anyway) is that I also have some javascript populating the field with http:// onfocus and removing onblur and I need to stop the url validation if the field is left blank but what is happening at the moment is that as soon as the focus is lost the form still looks for a valid url as the field has been focused on (hope some of this makes sense.

The code (I have removed all other rules for clarity)

// Pre-fill Online Profile URL form field with http:// on focus and remove on blur
	var text = "http://";
	
	$("#online_profile_url").focus(function() {
		if($(this).attr("value") == "") $(this).attr("value", text);
	});
	
	$("#online_profile_url").blur(function() {
		if($(this).attr("value") == text) $(this).attr("value", "");
	});
$("#appForm").validate({
		
		rules: {
			
			online_profile_url: "url"
			
		}
		
	});

How can I only run the url validation if more than just the http:// has been input in the field (I don't know if and how to set conditionals within the rules)?

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Apparently you can use 'depends'. See here on SO.

Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

Thanks, I will have a play with that

Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
1
 

Thank you so much @pritaeas I have implemented the function and it appears to be working brilliantly.

online_profile_url: { 
				url: {
					depends: function () {
						return $("#online_profile_url").attr("value") !== 'http://';
					}
				}
			}
Question Answered as of 2 Years Ago by pritaeas
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article