?
Solved

Regular expression javascript - validate name of company with accents/others

Posted on 2013-01-06
6
Medium Priority
?
2,010 Views
Last Modified: 2013-01-06
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
0
Comment
Question by:joao_c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 38748331
How about this:

companyname.match(/\S.{0,78}\S/)

Open in new window


\S matches for everything expect whitespaces.

If you trim your companyname using this command before:
companyname = companyname.replace(/^\s+|\s+$/g,"");

Open in new window

0
 

Author Comment

by:joao_c
ID: 38748339
thanks for the reply.

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 {
(...)

Open in new window

0
 
LVL 10

Expert Comment

by:acbxyz
ID: 38748355
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:
inName.value.replace(/^\s+|\s+$/g, '');
if (!inName.value.match(/^\S.{0,78}\S$/) {
  alert('please enter a valid name');
}

Open in new window

0
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 

Author Comment

by:joao_c
ID: 38748397
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/;

Open in new window

0
 
LVL 10

Accepted Solution

by:
acbxyz earned 1200 total points
ID: 38748402
This regex limits to 80 characters like your first one. The \S are two characters, adding the number in the brackets gives the maximum length.
If you want to limit to 60 characters the 78 must be a 58 (60-2).
To have this working, you need ^ and $ around your regex => /^\S.{0,58}\S$/. I missed those the first time, sorry.
0
 

Author Closing Comment

by:joao_c
ID: 38748425
Thanks, it works perfect.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question