2

I'm working in php with codeigniter and i need to so some form validation. Specifically, using Codeigniter's validation form library i coded this for name field:

        $this->form_validation->set_rules('name', 'Nombre', 'required|min_length[2]|max_length[50]|regex_match[/^([a-z,A-Z,á,é,í,ó,ú,â,ê,ô,ã,õ,ç,Á,É,Í,Ó,Ú,Â,Ê,Ô,Ã,Õ,Ç,ü,ñ,Ü,Ñ," "]+)$/]');

As you can see it accepts international characters for different kinds of names.

Now i want to do the same but on the front, making the same regular expression in jQuery.

I am using pattern method form additional-methods.js so this is the snippet from it to "build" the regular expression:

jQuery.validator.addMethod("pattern", function(value, element, param) {
if (this.optional(element)) {
    return true;
}
if (typeof param === 'string') {
    param = new RegExp('^(?:' + param + ')$');
}
return param.test(value);
}, "Formato Inválido.");

Then on my js file what i have is this (snippet):

 form2.validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-inline', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            ignore: "",
            rules: {                  
                name: {
                    minlength: 2,
                    maxlength: 50,
                    pattern: "[\s,.'-]*[a-zA-Z\pL][\s,.'-]*", //not working
                    required: true
                },

I've tried with many regular expressions, but it's not working. It also has to accept accents like: mathías, damián, and so on.

Can anyone give me a hand? thanks

rae1
  • 6,066
  • 4
  • 27
  • 48
Limon
  • 1,772
  • 7
  • 32
  • 61
  • 1
    Trying to "validate" names is one of the fundamental mistakes every programmer does once in his life. Humans have perfectly legal names that will not fit your validation. See http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – deceze Jan 16 '14 at 19:02
  • I guess that is pretty subjective. There are weird names, that's true. But only depending where they come from... in my country you don't see that very often so a validation like that is needed. Plus it's not something i can decide, standards from the project does. – Limon Jan 16 '14 at 20:27
  • i don't see the point of the argue here. You should do what suites you, adn i should do what suites me and the project. xoxo – Limon Jan 16 '14 at 21:05

4 Answers4

3

I'm gonna answer my question since i could find out what was doing wrong.

First i've tried with a expression i alread had which is the one i wrote before:

$this->form_validation->set_rules('name', 'Nombre', 'required|min_length[2]|max_length[50]|regex_match[/^([a-z,A-Z,á,é,í,ó,ú,â,ê,ô,ã,õ,ç,Á,É,Í,Ó,Ú,Â,Ê,Ô,Ã,Õ,Ç,ü,ñ,Ü,Ñ," "]+)$/]');

moving that to javascript didn't validate as i wanted, but the error was in the space: " "

i just replaced it with ' ' and that's it. So the final regular expression for latin names that i was looking for was just in front of my eyes:

pattern: "[a-z,A-Z,á,é,í,ó,ú,â,ê,ô,ã,õ,ç,Á,É,Í,Ó,Ú,Â,Ê,Ô,Ã,Õ,Ç,ü,ñ,Ü,Ñ,' ']+",

using pattern method from additional-methods.js

Limon
  • 1,772
  • 7
  • 32
  • 61
1

Try matching them by their unicode character expression by using \un, which "Matches n, where n is a Unicode character expressed as four hexadecimal digits. For example, \u00A9 matches the copyright symbol".

user2932397
  • 184
  • 8
  • I'm sorry that goes beyond my knowledge. I'm really new at this – Limon Jan 16 '14 at 18:42
  • All characters have a unicode representation which can be expressed as four hexadecimal digits. You need to find what this is for the characters you wish to check for. You could use charmap.exe on any windows machine, or a site like http://www.marathon-studios.com/unicode/ to find this out. For example, á is Latin Small Letter A With Acute, which is 00E1, so to match this, instead of á you would use \u00E1 – user2932397 Jan 16 '14 at 18:51
  • or google to find the unicode representation. like this: https://www.google.com/search?q=%C3%A1+unicode – user2932397 Jan 16 '14 at 18:52
  • of course. But wont that be too extensive for the thousands characters? isn't a more general way to achieve this? – Limon Jan 16 '14 at 18:58
  • To match this, you only need 26 of them. $this->form_validation->set_rules('name', 'Nombre', 'required|min_length[2]|max_length[50]|regex_match[/^([a-z,A-Z,á,é,í,ó,ú,â,ê,ô,ã,õ,ç,Á,É,Í,Ó,Ú,Â,Ê,Ô,Ã,Õ,Ç,ü,ñ,Ü,Ñ," "]+)$/]'); – user2932397 Jan 16 '14 at 19:06
  • I do not know of any way to match "any character with a mathías" or any other accent. perhaps you could match a range of unicode characters or instead use a black list (black lists are in general bad practice) – user2932397 Jan 16 '14 at 19:08
  • No, i just wrote mathías in order to represent í (as an example) – Limon Jan 16 '14 at 19:30
1

Nice answer but you're missing some characters in your set... I use this instead
var pattern = "^([a-zÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ\\s])+$"

ghiscoding
  • 12,308
  • 6
  • 69
  • 112
  • Thank you I will try it! :) I didn't use several of the characters that you used because the site I'm working on points to people who speak Spanish, and it's very weird to find a name with some of this rare letters. But let me try and see if I change that! – Limon Feb 06 '14 at 15:31
  • 1
    This is a much briefer string which achieves the same objective. I.e. it includes all the international characters that would be present in ASCII in the ASCII range from character 192 to 255: [@Limon](http://stackoverflow.com/users/2689507/limon) [@ghiscoding](http://stackoverflow.com/users/1212166/ghiscoding) [@user2932397](http://stackoverflow.com/users/2932397/user2932397) `([^A-zÀ-ÿ])` (I left out the curious \\s which may intend to catch white spaces but would not do so, as it actually would escape \ to include it and include s again) – Neil Dunlop Aug 03 '16 at 06:14
  • Thanks I didn't know about the `À-ÿ` range. The `\\s` is because the pattern is declared as a string type, so it has to be escaped and it does catch white space. – ghiscoding Aug 03 '16 at 17:16
0

Refernece link

/^[a-zA-Z\-_ ’'‘ÆÐƎƏƐƔIJŊŒẞÞǷȜæðǝəɛɣijŋœĸſßþƿȝĄƁÇĐƊĘĦĮƘŁØƠŞȘŢȚŦŲƯY̨Ƴąɓçđɗęħįƙłøơşșţțŧųưy̨ƴÁÀÂÄǍĂĀÃÅǺĄÆǼǢƁĆĊĈČÇĎḌĐƊÐÉÈĖÊËĚĔĒĘẸƎƏƐĠĜǦĞĢƔáàâäǎăāãåǻąæǽǣɓćċĉčçďḍđɗðéèėêëěĕēęẹǝəɛġĝǧğģɣĤḤĦIÍÌİÎÏǏĬĪĨĮỊIJĴĶƘĹĻŁĽĿʼNŃN̈ŇÑŅŊÓÒÔÖǑŎŌÕŐỌØǾƠŒĥḥħıíìiîïǐĭīĩįịijĵķƙĸĺļłľŀʼnńn̈ňñņŋóòôöǒŏōõőọøǿơœŔŘŖŚŜŠŞȘṢẞŤŢṬŦÞÚÙÛÜǓŬŪŨŰŮŲỤƯẂẀŴẄǷÝỲŶŸȲỸƳŹŻŽẒŕřŗſśŝšşșṣßťţṭŧþúùûüǔŭūũűůųụưẃẁŵẅƿýỳŷÿȳỹƴźżžẓ]$/.test(myString)

This regex accept all international characters

Kumar Shanmugam
  • 597
  • 1
  • 11
  • 40