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

Recommended Answers

You need to escape the . and -

return this.optional(element) || /^[a-zA-Z0-9\._\-]{3,16}$/i.test(value);
Jump to Post

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

Jump to Post

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 …

Jump to Post

All 11 Replies

You need to escape the . and -

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

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.

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

You need to escape the . and -

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

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

My bad, you're right.

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

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

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.

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

However, I have an idea. I think the field 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].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.