2

I'm using the jQuery.validate plugin to validate inputs for my user registration form.

I've got the simple case working(make sure user name is at least 6 characters)

$('#new_user').validate({
    rules: {
        "user[name]": {
            required: true,
            minlength: 6,
            remote:"/check_name"
        }, ...

One caveat I'd like to add to the above rule is to allow a minimum of 2 Chinese characters OR 6 regular characters to pass the length test. (This is considered good usability for Chinese users, many of whom has 2-3 Chinese Character names in real life)

For example, I'd like the following names to pass the length test:

"张三" "Michael"

and the following names to fail(due to being too short) "张" "Mike"

How do you write a custom javascript function to check for these conditions?

3
  • 2
    What about names, such as "Lee," that are shorter than six characters? How about trusting your users to enter their name correctly, and check that they've simply not left an empty field? Mar 6, 2011 at 17:02
  • Oh and I think many Chinese people have a 1-character name.
    – ysdx
    Mar 6, 2011 at 22:17
  • @David Thomas good point. I noticed most sign up forms has a minlength set to at least 4-6, which made me think that it was bad form to allow names such as "aa", but your question do make me think twice on the UX design. Thank you.
    – tstyle
    Mar 7, 2011 at 5:14

2 Answers 2

4

Something like :

  function validate(string) {
    var re = /^[\u3300-\u9fff\uf900-\ufaff]{2,}$/;
    return string.length >= 6 || string.match(re);
  }

  validate("pat")
  validate("patate")
  validate("中")
  validate("中国")

See the Unicode Roadmap for details [1]

[1] http://unicode.org/roadmaps/bmp/

1
  • Thanks this is (almost) exactly what I was looking for. Ideally the regex would exclude weird characters as well(and would only allow letters, numbers, and Chinese characters, and not spaces or weird symbols like @#$@
    – tstyle
    Mar 7, 2011 at 5:01
1

You can use the internationalization plugin so that you can do this test:

function returnMinLength(){
    var browserLanguage = jQuery.i18n.browserLang();
    if (browserLanguage == "zh") // zh stands for chinese (I'm not sure)
       return 2;
    else
       return 6;
}

Then, in your function:

   rules: {
        "user[name]": {
            required: true,
            minlength: returnMinLength(),
            remote:"/check_name"
        }, ...

Regards.

4
  • Other languages can user chinese characters (like Japanese) and might want to have two character names. And Chinese people might use browsers in English (for example). Apart from that, I agree with David Thoma. If you really want to do this (again this does not look lika a really goode idea), you should check the characters.
    – ysdx
    Mar 6, 2011 at 19:48
  • Ok. Here is nice script that checks if the input is in Chinese or not (it tests on a range of unicodes):
    – Zakaria
    Mar 6, 2011 at 19:55
  • Don't understand the script. Why is there two Regexp and not only one? Why is there a range if the "Private User Zone" ? I'de say : ^[\u3300-\u9fff\uf900-\ufaff]*$ You might want to include things like Hiragana/Katakana (if you want to include Japaneses) and Hangul (for Korean) as well.
    – ysdx
    Mar 6, 2011 at 20:24
  • Thanks for the alternative answer! The custom check function, rather than dynamically computing the minlength: works better for my particular scenario.
    – tstyle
    Mar 7, 2011 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.