joao_c
asked on
Regular expression javascript - validate name of company with accents/others
Hello everyone!
I need a regex in javascript that can perform a validation of a company name.
I curently have " /^[A-Za-z0-9 ]{2,80}$/ ";
But this won't accept my country accents or spaces or special characters. Some examples of names that should accept:
-João Company
-Leão@
-édel Às
-4&you
-nº2
Also, it should check if it has at least 2 characters and not filled with whitespaces.
Basicly it allows any character but should not be filled with whitespaces only and should have at least 2 characters.
Thank you
I need a regex in javascript that can perform a validation of a company name.
I curently have " /^[A-Za-z0-9 ]{2,80}$/ ";
But this won't accept my country accents or spaces or special characters. Some examples of names that should accept:
-João Company
-Leão@
-édel Às
-4&you
-nº2
Also, it should check if it has at least 2 characters and not filled with whitespaces.
Basicly it allows any character but should not be filled with whitespaces only and should have at least 2 characters.
Thank you
ASKER
thanks for the reply.
I am using the .test method, how can i store what u suggested in a variable(var inNameReg)?
I am using the .test method, how can i store what u suggested in a variable(var inNameReg)?
$('form').on('submit', function() {
var info = $('#infoSubmit');
var inName = $('input#name-of-company');
var inNif = $('input#nif');
var inNameReg = /^[A-Za-z0-9 ]{2,80}$/;
if ( !inNameReg.test(inName.val()) ) {
info.text('please enter a valid name');
inName.addClass('wrongInput');
inName.focus();
} else {
(...)
I never used regex.test(value), but it should work the same way you wrote.
var inNameReg = /\S.{0,78}\S/;
I can't say anything about inName.val() because it is part of a js-framework I don't know or use. I'd use:
var inNameReg = /\S.{0,78}\S/;
I can't say anything about inName.val() because it is part of a js-framework I don't know or use. I'd use:
inName.value.replace(/^\s+|\s+$/g, '');
if (!inName.value.match(/^\S.{0,78}\S$/) {
alert('please enter a valid name');
}
ASKER
Thanks, but what {0,78} means? I see that I need to enter at least to characters and that's fine. But I also want to limit to 60 characters. I see the limit is not working in the current regex:
var inNameReg = /\S.{0,78}\S/;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, it works perfect.
Open in new window
\S matches for everything expect whitespaces.
If you trim your companyname using this command before:
Open in new window