[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Most efficient javascript leading/trailing space checker

Posted on 2006-03-24
5
Medium Priority
?
764 Views
Last Modified: 2008-03-17
To 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>
0
Comment
Question by:crimson117
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16284255
<script language="javascript">
var str = '      Leading ...             ..    Trailing ....         ';
alert ('STR <' + str + '>');
// trim
var str2 = str.replace(/^(\s{1,})|(\s{1,})$/g, '');

// replace two spaces with one space

var str2 = str2.replace (/(\s{2,})/g, ' ');
alert ('STR <' + str2 + '>');
</script>
0
 
LVL 5

Author Comment

by:crimson117
ID: 16284494
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,"") faster?
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 16284599
You can use any of the match

I will say this is efficient

if (str.match (/^(\s{1,})|(\S{1,})(\s{2,})(\S{1,})|(\s{1,})$/))
{
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,})/)) { alert ('Found More than 2 spaces.'); }

if (str.match (/^(\s{1,})|(\S{1,})(\s{2,})(\S{1,})|(\s{1,})$/))
{
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{1,})$|(\s{2,})/g, '');

// replace two spaces with one space

//alert ('STR <' + str2 + '>');
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 1600 total points
ID: 16300503
>> 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,"") faster?


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,})(\S{1,})|(\s{1,})$/))
{
alert ('Found Either\n Leading Spaces\n or \nMore than 2 spaces.\n or \nTrailing space\n');
}


0
 
LVL 5

Author Comment

by:crimson117
ID: 16301001
>>Do match is much faster and efficient. than any of your method (indexOf or replace),

Perfect, thanks Pravinasar!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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

826 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