1

So basically I'm trying to validate URLs (http://www.x) - currently working - and on top of that, validate jsFiddle (input must start with "http://www.jsfiddle.net/") & stackoverflow URLs (input must start with "http://www.stackoverflow.com/")

Here is a working fiddle http://jsfiddle.net/eAnS9/

The validation is the bassistance validate plugin, which is documented here.

Any idea how to validate the fields correctly based on their beginning values?

UPDATE: It must work with/integrate with bassistance current validation, maybe as a custom url function in bassistance source code? (there is where the url regex is)

2 Answers 2

2

Use validator.addMethod() to create a custom rule. This one should do the trick:

//--- Add a domain-check rule
jQuery.validator.addMethod ("domainChk", function (value, element, params) {

        if (this.optional (element) )
            return true;

        //--- Get the target domain, this will be in the rel attribute.
        var targDomain  = $(element).attr ("rel");
        var regExp      = new RegExp ("^https?:\/\/(www.)?" + targDomain + "\/", "i");

        return regExp.test (value);
    },
    function errmess (params, element) {
        return "This must be a valid URL for " + $(element).attr ("rel");
    }
);

Now connect it to a CSS class. :

jQuery.validator.addClassRules ( { domainChk: {domainChk: true} } );


The domainChk rule expects the required domain to be in the rel attribute of the input, like so:

<input class="field required url domainChk" rel="jsfiddle.com"      ...>
<input class="field required url domainChk" rel="stackoverflow.com" ...>


That's it!   See it all in action at jsFiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer. Thanks very much Brock, I learned a lot today:)
0

could be better but

   var Reg = /https?:\/\/(w{3}\.)?(jsfiddle|stackoverflow)(.com\/)/

is what i used. http://jsfiddle.net/eAnS9/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.