Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to to check for " 1

Status
Not open for further replies.

spicymango

Programmer
May 25, 2008
119
CA
I am checking to see my string has any of the following characters. The way I am doing it, I can test for all except double quotes ". If I put double quote that beaks my string.


I don't know the way around it.

Code:
<script>


var frenchdisplaystring="";
var setFrenchFlag="";
var french = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ’ñ€Æ»«æ'.;-&";
var name = ".;-&kitty 12àÉ'";

//looping through the text that needs to be validated
for(var i=0; i<name.length; i++) {
	var namechar = name[i];
	
        //looping through the French characters
		for(var a=0; a<french.length; a++) {
	    var frenchchar = french[a];

        if (namechar == frenchchar )
        {
          setFrenchFlag = "1";
		  frenchdisplaystring = frenchdisplaystring + "  \\"+frenchchar;
		}
     
  }
}
alert(frenchdisplaystring);

</script>
 
I don't know regex, plus I also want to get the value that matches so that I can alert it.

I was hoping I could add " in my string, but I guess I can not.
 
spicymango said:
I don't know regex

Lazy is as lazy does...

If you are in fact a "Programmer" as your profile shows, you should learn regular expressions...one of the (if not the) most useful tools out there for searching/parsing strings.




--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
I know I should learn regex.

But life is more then learning IT stuff and die one day.
 
Hi

spicymango, I see that you also had Apache, PHP, Java servlet and Unix scripting related activity. Regular expressions helped me a lot in those topics too. And I had to learn them only once.

Personally I would prefer this instead of looping over the strings, character by character :
Code:
[b]var[/b] array_of_found_french_characters[teal]=[/teal]name[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/[àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ'ñ#Æ»«æ'.;\-&]/g[/fuchsia][teal])[/teal]
Also note that your code finds and displays the single quote ( ' ) twice, because you enumerated it twice in the french string. I kept it doubled in the regular expression, but the result is still correct, displaying the single quote as found only once.

Hopefully you will be able to include the double quote ( " ) without problem.

Feherke.
 
Hi,

Thanks for your message, your way for sure is better. In my case I need to display what matched in this format

\À \& \æ

but when I do alert on your I get it like
À,&,æ

That's why I was looping, is there a way one can customize display, and display it my way
 
Hi

spicymango said:
but when I do alert on your I get it like
À,&,æ
Well, in my code array_of_found_french_characters is an [tt]Array[/tt] object. When objects are treated as strings, their [tt]toString()[/tt] method is called implicitly. And that is what [tt]Array.toString()[/tt] does : just calls the [tt]join()[/tt] method.

If you overwrite the default [tt]toString()[/tt] method, it will be formatted as you want :
Code:
Array[teal].[/teal][b]prototype[/b][teal].[/teal]toString[teal]=[/teal][b]function[/b][teal]()[/teal] [teal]{[/teal]
  [b]return[/b] [green][i]' [/i][/green][lime][i]\\[/i][/lime][green][i]'[/i][/green][teal]+[/teal][b]this[/b][teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]' [/i][/green][lime][i]\\[/i][/lime][green][i]'[/i][/green][teal])[/teal]
[teal]}[/teal]
( Note that this affects all [tt]Array[/tt] stringifications. )
spicymango said:
That's why I was looping,
Yep, I was not clear enough.

There is nothing wrong in looping over the matches, as they are just a few. But looping over both strings all characters is abit ugly.

Anyway, you can avoid the explicit looping :
Code:
string_to_alert[teal]=[/teal]array_of_found_french_characters[teal].[/teal][COLOR=darkgoldenrod]reduce[/color][teal]([/teal][b]function[/b][teal]([/teal]p[teal],[/teal]c[teal])[/teal][teal]{[/teal][b]return[/b] p[teal]+[/teal][green][i]' [/i][/green][lime][i]\\[/i][/lime][green][i]'[/i][/green][teal]+[/teal]c[teal]}[/teal][teal],[/teal][green][i]''[/i][/green][teal])[/teal]

[gray]// or[/gray]

string_to_alert[teal]=[/teal]array_of_found_french_characters[teal].[/teal][COLOR=darkgoldenrod]map[/color][teal]([/teal][b]function[/b][teal]([/teal]c[teal])[/teal][teal]{[/teal][b]return[/b] [green][i]' [/i][/green][lime][i]\\[/i][/lime][green][i]'[/i][/green][teal]+[/teal]c[teal]}[/teal][teal]).[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]''[/i][/green][teal])[/teal]
( But one major browser still not supports those [tt]Array[/tt] methods. Guess which one. See ECMAScript 5 compatibility table. )
spicymango said:
is there a way one can customize display
I would simply [tt]join()[/tt] them :
Code:
string_to_alert[teal]=[/teal][green][i]' [/i][/green][lime][i]\\[/i][/lime][green][i]'[/i][/green][teal]+[/teal]array_of_found_french_characters[teal].[/teal][COLOR=darkgoldenrod]join[/color][teal]([/teal][green][i]' [/i][/green][lime][i]\\[/i][/lime][green][i]'[/i][/green][teal])[/teal]

Feherke.
 
Hi feherke,

With your help I am doing the following

Code:
<script>



var french = "àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ'ñ€Æ»«æ'.;-&";
var name = '.;-&kitty 12à"';



var array_of_found_french_characters=name.match(/[àÀâÂäÄáÁéÉèÈêÊëËìÌîÎïÏòÒôÔöÖùÙûÛüÜçÇ'ñ#Æ»«æ".;\-&]/g)
string_to_alert=' \\'+array_of_found_french_characters.join(' \\')



alert("At least one of your account names has special characters (" + string_to_alert + "). Please remove them and then save your changes.");
</script>


Now did you mean this join() is not supported by major browser? I tried on IE and FireFox it worked.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top