That's a way to strip leading, trailing, and to convert 2 spaces to 1 space.
I'm looking for an answer on what method of checking is most efficient?
Is indexOf faster, or name.replace(/^\s*|\s*$/g,
Main Topics
Browse All TopicsTo vaildate name fields, I need to check that there are no two spaces in a row, and no leading or trailing spaces at all.
I've put together these two methods. Which is more efficient? Or is there an even more efficient alternative?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<script>
name = 'j onny g ';
//method 1
if (name.indexOf(' ') != -1 ||
name.indexOf(' ') == 0 ||
name.indexOf(' ') == name.length-1 )
document.write('failed1');
else
document.write('passed1');
document.write('<br>');
//method 2
if (name.indexOf(' ') != -1 ||
name != name.replace(/^\s*|\s*$/g,
document.write('failed2');
else
document.write('passed2');
</script>
</BODY>
</HTML>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can use any of the match
I will say this is efficient
if (str.match (/^(\s{1,})|(\S{1,})(\s{2,
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}
<script language="javascript">
var str = ' Leading ... .. Trailing .... ';
alert ('STR <' + str + '>');
if (str.match (/^(\s{1,})/)) { alert ('Found Leading spaces'); }
if (str.match (/(\s{1,})$/)) { alert ('Found Trailing spaces'); }
if (str.match (/(\S{1,})(\s{2,})(\S{1,})
if (str.match (/^(\s{1,})|(\S{1,})(\s{2,
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}
// trim
//var str2 = str.replace(/^(\s{1,})|(\s
// replace two spaces with one space
//alert ('STR <' + str2 + '>');
>> I've put together these two methods. Which is more efficient? Or is there an even more efficient alternative?
>>That's a way to strip leading, trailing, and to convert 2 spaces to 1 space.
>>I'm looking for an answer on what method of checking is most efficient?
>>Is indexOf faster, or name.replace(/^\s*|\s*$/g,
Do match is much faster and efficient. than any of your method (indexOf or replace),
This one line of code, do all the job..
if (str.match (/^(\s{1,})|(\S{1,})(\s{2,
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}
Business Accounts
Answer for Membership
by: pravinasarPosted on 2006-03-24 at 13:07:11ID: 16284255
<script language="javascript"> {1,})$/g, '');
var str = ' Leading ... .. Trailing .... ';
alert ('STR <' + str + '>');
// trim
var str2 = str.replace(/^(\s{1,})|(\s
// replace two spaces with one space
var str2 = str2.replace (/(\s{2,})/g, ' ');
alert ('STR <' + str2 + '>');
</script>