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
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
Share Improve this question edited Jan 16, 2014 at 18:44 rae1 6,1425 gold badges29 silver badges49 bronze badges asked Jan 16, 2014 at 18:35 LimonLimon 1,7937 gold badges33 silver badges63 bronze badges 3- 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 kalzumeus./2010/06/17/… – deceze ♦ Commented Jan 16, 2014 at 19:02
- I guess that is pretty subjective. There are weird names, that's true. But only depending where they e 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 Commented Jan 16, 2014 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 Commented Jan 16, 2014 at 21:05
4 Answers
Reset to default 3I'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
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".
Nice answer but you're missing some characters in your set... I use this instead
var pattern = "^([a-zÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ\\s])+$"
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