First, that should be: addMethod("buga", function(value, element, regex) { var expression = new RegExp(regex, "g"); return (value.replace(expression,"").length==0); }
Second, just the same as your initial draft: someElement: { required: true, buga: "[a-z]" } But that wasn't the point, rather this: addMethod("letters", function(value) { return /[a-z]/.test(value); } Then: someElement: { required: true, letters: true } That way you give the regex a name and make it reusable. Most non-trivial regular expressions are write only - you really don't want to try to read and understand them. So having them in one place only and with a name is well worth it. Jörn On Mon, Dec 8, 2008 at 7:31 PM, skidmarek <[EMAIL PROTECTED]> wrote: > > For example: > > addMethod("buga", function(value, regex) { > var expression = new RegExp(regex, "g"); > return (value.replace(expression,"").length==0); > } > > how then would I call buga from the rules section of my form > definition? > > > On Dec 8, 11:49 am, "Jörn Zaefferer" <[EMAIL PROTECTED]> > wrote: >> I'm not following your question. Could you give me an example? >> >> Jörn >> >> On Mon, Dec 8, 2008 at 5:24 PM, skidmarek <[EMAIL PROTECTED]> wrote: >> >> > I've changed my code to use addMethod, but I'm not terribly happy with >> > the way it's working out. Would it be possible to use addMethod to >> > create a validation function to which I could pass a 2nd parameter >> > being the regular expression. If so, what would the rules: entry look >> > like? >> >> > On Dec 5, 5:14 pm, "Jörn Zaefferer" <[EMAIL PROTECTED]> >> > wrote: >> >> You can safely remove the first modification, thats not necessary. Its >> >> used for required only to move the method to the front (which is >> >> actually a bad workaround). Anyway: >> >> >> I've commented on regex-methods >> >> here:http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemet... >> >> >> Please note: While the temptation is great to add a regex method that >> >> checks it's parameter against the value, it is much cleaner to >> >> encapsulate those regular expressions inside their own method. If you >> >> need lots of slightly different expressions, try to extract a common >> >> parameter. A library of regular >> >> expressions:http://regexlib.com/DisplayPatterns.aspx >> >> >> Jörn >> >> >> On Fri, Dec 5, 2008 at 8:01 PM, skidmarek <[EMAIL PROTECTED]> wrote: >> >> >> > I've created a useful addition to your class that you may want to >> >> > include in a future version. >> >> >> > Set the value of the regex attribute to a regular expression. If any >> >> > characters in the input don't match the regex, it returns false. Very >> >> > handy. >> >> >> > Here's the source: >> >> >> > ------------------------------------------- In rules: >> >> >> > if (data.regex) { >> >> > var param = data.regex; >> >> > delete data.regex; >> >> > data = $.extend({regex: param}, data); >> >> > } >> >> >> > ------------------------------------------- Then in methods: >> >> >> > regex: function(value,element,param) { >> >> > if (param) { >> >> > var expression = new RegExp(param, "g"); >> >> > return (value.replace(expression,"").length==0); >> >> > } else { >> >> > return true; >> >> > } >> >> > }, >> >> >> > ------------------------------------------- Usage: >> >> >> > regex: "\\b[a-zA-Z0-9()[EMAIL PROTECTED]"'?&* ]+\\b" >> >> >> > If the input contains any characters that aren't in that list, it >> >> > validates as false. > > >