954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding "validator.addMethod", but dosn't work, any help guys

Hi Jquery Guros,
I develop registeration form (in arabic).
I added "validator.addMethod" to check the firstname text.
I want to check that the "firstname" text must be in INGLISH leters OR Numbers(means, user can't insert arabic leters or anything else).

I added the script:
///////// chack firstname (English leter OR numbers)

$.validator.addMethod("firstname",function(value,element){
return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
},"Username are 3-15 characters");

:(

You can check the script at: http://www.alnaddy.com/ldap/index1.php

Can anybody help me guys?

Thanks a lot.
Sam.


But dosn't work

sammeras
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

You need to escape the . and -

return this.optional(element) || /^[a-zA-Z0-9\._\-]{3,16}$/i.test(value);
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thanks pritaeas for your replay.
Unfortunately, not workong, I still can type arabic letters in the firstname box, No error alert (in red) dispalyed on the screen that tell me to use just ENGLISH letter OR numbers.

Any aideas?

Thanks again.

sammeras
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

What does this.optional(element) do ? If this returns true, then the regex is not validated, because of the || (or).

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

The code work will in this test link:
http://www.alnaddy.com/ldap/test/
(See the code -- view source)

sammeras
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 
You need to escape the . and -

That is not correct. Those characters are used as themselves in that character class.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 
That is not correct. Those characters are used as themselves in that character class.

My bad, you're right.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

It's Ok pritaeas, What i forgot/do wrong in my?

sammeras
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Can no longer open your pages... I'll try again tonight.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Both pages
http://www.alnaddy.com/ldap/test/
http://www.alnaddy.com/ldap/index1.php
apply the minLength rule, so your code is not completely broken.

The page that actually tests the characters
http://www.alnaddy.com/ldap/test/
has charset=iso-8859-1

Your page http://www.alnaddy.com/ldap/index1.php
has charset=utf-8

I'm fairly sure that difference affects regexp evaluation (especially with the .i flag).

At the moment www.alnaddy.com is down from here, so can't explore this matter further right now.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

Hi, thanks for your answer, I changed the charset to utf-8 in the:
http://www.alnaddy.com/ldap/test/

It's work well.

Any ideas to fuxing the issues?

Thanks guys.
Sam

sammeras
Newbie Poster
8 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Caution: I only started reading about jquery when you posted this question.

However, I have an idea. I think the field ['firstname'] must have a rule for your method [which I have renamed 'getNick']. I am assuming that this.optional(element) will return false [because the field is marked required: true]. If there is any doubt I would leave that out of the return [because as I understand your form this field cannot be optional].

My suggested fix adds what I think is the proper way to reference your function in the ruleset for firstname. If I'm wrong on this point, I hope someone will correct the code.

Hope this helps, and please let me know what happens.

///////// chack firstname (English leter OR numbers
$.validator.addMethod("getNick", function (value, element) {
    return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
}, "Username are 3-15 characters");
rules: {
        firstname: {
            required: true,
            minLength: 3,
            getNick: true
        },


and this

return /^[a-zA-Z0-9._-]{3,16}$/i.test(value);


would be the change to the return [to 'simplify' it].

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: