Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jQuery validate , depends function error? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I'm not sure if I'm doing it right, but I'm trying to add some regex validation to the rules.

I assume this is done via the depends mechanism...

Code:
             //form validation rules
             $("#myform").validate({
                 rules: {
                 
                    ID: {
                        required: true,
                        [b]depends: function(element) {
                                return (element.value.match(/[^0-9a-z\-\_]/gi) == '')                                
                        }[/b]                    
                    }
                   
                 },
                 
                 messages: {
                    ID: {
                        required: "Please enter a valid UserID",
                        depends : "Invalid characters found, only use (letters, numbers, underscore or hyphen)"
                        }                      
                     
                 },
                 
                 submitHandler: function(form) {
                     form.submit();
                 }
                 
             });

but that just errors with
Error: TypeError: c.validator.methods[e] is undefined
Source File: Line: 29
in the FF error console and the form is still submitted?

What am I doing wrong?

Is there an easier / better way of validating acceptable input using the jQuery validate plugin?

I cannot see a way mentioned in the documentation.

Thanks,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
hi. the javascript match method of the regexp object returns NULL if no matches exist, or an array if there are matches. consider using test instead.

or just this
Code:
[b][COLOR=#0000FF]return[/color][/b] element[COLOR=#990000].[/color]value[COLOR=#990000].[/color][b][COLOR=#000000]match[/color][/b][COLOR=#990000]([/color][COLOR=#FF6600]/[^0-9a-z\-\_]/gi[/color][COLOR=#990000])[/color] [COLOR=#990000]===[/color] NULL[COLOR=#990000];[/color]

using test

Code:
[b][COLOR=#0000FF]return[/color][/b] [COLOR=#990000]![/color][COLOR=#FF6600]/[^0-9a-z\-\_]/gi[/color][COLOR=#990000].[/color][b][COLOR=#000000]test[/color][/b][COLOR=#990000]([/color]$[COLOR=#990000]([/color]element[COLOR=#990000]).[/color][b][COLOR=#000000]value[/color][/b][COLOR=#990000]());[/color]
 
Thanks, but I'm using it wrong anyway.

the depends is a switch to exisiting validation not one on its own - rereading the documentation.


Code:
required: function(element) {
        return $("#age").val() < 13;
      }

or

email: {
         depends: function(element) {
           return $("#contactform_email:checked")
         }
}

So it only sets required on a particular element if the return is true.

I can't see any validation method that is for allowed chars or a boolen regex , can you?

I want something like an allow method or something I can evaluate to boolen as to whether the input is valid.

for example...
Code:
//form validation rules
             $("#myform").validate({
                 rules: {
                 
                    ID: {
                        required: true,
[b]                        valid: {
                                depends: function(element) {
                                  return !(/[^0-9a-z\-\_]/gi.test(element.value));                                 
                                }[/b]                               
                        }                    
                    }
                   
                 },
                 
                 messages: {
                    ID: {
                        required: "Please enter a valid UserID",
                        valid: "Invalid characters found, only use (letters, numbers, underscore or hyphen)"
                        }                      
                     
                 },
                 
                 submitHandler: function(form) {
                     form.submit();
                 }
                 
             });

So if valid is true all is ok otherwise diplay the relevant message.

Though I have no idea how I would implement it?




"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
yes. the depends is a filter for whether the rule applies. i guess you were thinking it could be a custom validation rule?

if you want that then you first have to register a new handler. then use the handler like any other validation method (i.e. in the attributes)

Code:
$[COLOR=#990000].[/color]validator[COLOR=#990000].[/color][b][COLOR=#000000]addMethod[/color][/b][COLOR=#990000]([/color]  [COLOR=#FF0000]"alphanumeric"[/color][COLOR=#990000],[/color] 
[tab][tab][tab][b][COLOR=#0000FF]function[/color][/b][COLOR=#990000]([/color]value[COLOR=#990000],[/color] element[COLOR=#990000])[/color] [COLOR=#FF0000]{[/color] 
[tab][tab][tab][tab][b][COLOR=#0000FF]return[/color][/b] [COLOR=#990000]([/color][COLOR=#FF6600]/[^0-9a-z\-\_]/gi[/color][COLOR=#990000].[/color][b][COLOR=#000000]test[/color][/b][COLOR=#990000]([/color] value [COLOR=#990000])[/color] [COLOR=#990000]===[/color] [b][COLOR=#0000FF]false[/color][/b][COLOR=#990000]);[/color]
[tab][tab][tab][COLOR=#FF0000]}[/color][COLOR=#990000],[/color]
[tab][tab][tab][COLOR=#FF0000]"Only alphanumeric characters are allowed"[/color]
[tab][tab][COLOR=#990000]);[/color]
 
well doing some digging, I'm on the right track, someone else on stack overflow is doing something similar
Code:
$.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var check = false;
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , : ; ? ' ' \" - = ~ ! @ # $ % ^ & * ( ) _ + / < > { } )"
);

So I guess I need to look at the addMethod feature and see if I can do similar...

Code:
$.validator.addMethod(
    "valid",
    function(value, element, valid) {
        return valid;
    }
    );

That looks promissing, the documentation has this note
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

But doing it the way I have suggested removes the regex as the function, I am simply adding a method that returns true or false from the regex.

Dunno, any thoughts on this?

Cheers,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
ummm ... i thought that's what i said? it's certainly what I meant to communicate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top