I noticed that some validation tools use the "class" attribute to pass options to validation scripts.
Example:
<input class="input-field validate[required, email]" name="" type="text" />
the "required" and "email" would be picked up by the script as setting options.
I was wondering is there an easy way to achieve this or maybe this is already available in jQuery? I want to use this as a means of passing certain settings to my script.
Any help would be appreciated, Thanks
Edit:
Thanks @loseb your example works great. Another thing I am trying to achieve by doing some small modification to your example:
function classAttributes( classString, className ) {
var data;
var regex = new RegExp(className+"\[(.*?)\]");
var matches = classString.match(regex);
if ( matches ) {
//matches[1] refers to options inside [] "required, email, ..."
var spec = matches[1].split(/,\s*/);
if ( spec.length > 0 ) {
data = spec;
}
}
return data;
}
any idea why "new RegExp(className+"[(.*?)]");" doesnt work. var matches is blank.
Edit: second part of the question moved to: javascript regex pattern with a variable string not working