Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2010, 05:29 PM   PM User | #1
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Special Character Replace with Jquery

Hi all,

I want to be able to replace all special characters like: ',",..., with their HTML entities.

I have no option to PHP which I have found online multiple options.

Here is an example as to what I'm dealing with.

<form id='myform'>

<input>There's so many options out there</input>

to....

<input> There&lsquo;s so many options out there</input>

</form>

Any help would be awesome thank you!
pcproff is offline   Reply With Quote
Old 09-21-2010, 01:38 AM   PM User | #2
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,155
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
I just went through all this, took about three hours to get everything working satisfactorily. I wanted people to add comments to a photo album, and I wanted them to be able to include some HTML.
Code:
$newarg1 = str_replace('"','&#34;',$arg1);
That's what I ended up with, just escaping the double quotes because that's what I was using to contain the text. If it escaped the single quotes people would not have been able to enter any HTML attributes.

The way this setup worked was they went to an editor page, chose a thumbnail by clicking on it, typed into a heading and message field and then clicked a form button to enter the data. when you opened up the editor the next time you would see all the text that you had entered and could edit it. I used the post method. I was very pleased with the result. A lot depends on how you are using and storing the data. In this case it was simply stored as a php file in the user's album. It was retrieved by printing the variables.

Last edited by DrDOS; 09-21-2010 at 01:45 AM..
DrDOS is offline   Reply With Quote
Old 09-21-2010, 11:45 AM   PM User | #3
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Sounds like the OP wants a javascript equivalent to the php function htmlentities. You could try this link.
SB65 is offline   Reply With Quote
Old 09-22-2010, 06:22 PM   PM User | #4
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Very newb question but how do I incorporate this to check my input fields in my form? Thank for your answer guys, trust me I have been looking for a while.

Last edited by pcproff; 09-22-2010 at 06:27 PM..
pcproff is offline   Reply With Quote
Old 09-22-2010, 06:26 PM   PM User | #5
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Quote:
Sounds like the OP wants a javascript equivalent to the php function htmlentities. You could try this link.
Very newb question but how do I incorporate this to check my input fields in my form? Thank for your answer guys, trust me I have been looking for a while.
pcproff is offline   Reply With Quote
Old 09-22-2010, 06:34 PM   PM User | #6
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Use the javascript functions given on the link (looks like you need htmlentities and get_html_translation_table) which then allow you to call a htmlentities function in javascript which you can use to output transformed text from your input box.
SB65 is offline   Reply With Quote
Old 09-22-2010, 08:02 PM   PM User | #7
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
This is what I got and no success.

<html>
<head>
<script src="php.default.min.js"></script>
<script type="text/javascript">
function htmlentities (string, quote_style) {
var hash_map = {}, symbol = '', tmp_str = '', entity = '';
tmp_str = string.toString();

if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
return false;
}
hash_map["'"] = ''';
for (symbol in hash_map) {
entity = hash_map[symbol];
tmp_str = tmp_str.split(symbol).join(entity);
}

return tmp_str;
}</script>
</head>
<body>
<h1>frequency decoder</h1>
<h2>Unicode &amp; HTML entity conversion service</h2>

<form action="" method="">
<fieldset>
<label for="hphrase">Enter phrase:</label>
<input type="text" name="hphrase" value="" tabindex="4" />
</fieldset>
<input type="button" value="Convert" tabindex="5" onClick="htmlentities();" />
</form>
</body>
</html>
pcproff is offline   Reply With Quote
Old 09-22-2010, 09:30 PM   PM User | #8
harbingerOTV
Senior Coder

 
Join Date: Jan 2005
Location: Memphis, TN
Posts: 1,765
Thanks: 8
Thanked 123 Times in 121 Posts
harbingerOTV will become famous soon enough
Code:
<form action="" method="">
<fieldset>
<label for="hphrase">Enter phrase:</label>
<input type="text" name="hphrase" id="hphrase" value="" tabindex="4" />
</fieldset>
<input type="button" value="Convert" tabindex="5" id="runit" />
</form>
Code:
$(document).ready(function(){
	$('#runit').click(function(){
		alert(htmlentities($('#hphrase').val()));
	});
});
and making sure you are loding the scripts from both:

http://phpjs.org/functions/htmlentities:425

and

http://phpjs.org/functions/get_html_...tion_table:416
__________________
Stop making things so hard on yourself.
i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis
harbingerOTV is offline   Reply With Quote
Old 09-22-2010, 10:02 PM   PM User | #9
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Quote:
Originally Posted by harbingerOTV View Post
Code:
<form action="" method="">
<fieldset>
<label for="hphrase">Enter phrase:</label>
<input type="text" name="hphrase" id="hphrase" value="" tabindex="4" />
</fieldset>
<input type="button" value="Convert" tabindex="5" id="runit" />
</form>
Code:
$(document).ready(function(){
	$('#runit').click(function(){
		alert(htmlentities($('#hphrase').val()));
	});
});
and making sure you are loding the scripts from both:

http://phpjs.org/functions/htmlentities:425

and

http://phpjs.org/functions/get_html_...tion_table:416
This works WONDERS! AWESOME THANKS! (sorry caps I've been on this a while)

I see that you are alerting #hphrase is there a way to replace the text inside #hphrase input fields?
pcproff is offline   Reply With Quote
Old 09-22-2010, 10:14 PM   PM User | #10
harbingerOTV
Senior Coder

 
Join Date: Jan 2005
Location: Memphis, TN
Posts: 1,765
Thanks: 8
Thanked 123 Times in 121 Posts
harbingerOTV will become famous soon enough
Code:
$(document).ready(function(){
	$('#runit').click(function(){
		$('#hphrase').val(htmlentities($('#hphrase').val()));
	});
});
__________________
Stop making things so hard on yourself.
i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis
harbingerOTV is offline   Reply With Quote
Old 09-22-2010, 10:23 PM   PM User | #11
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Quote:
Originally Posted by harbingerOTV View Post
Code:
$(document).ready(function(){
	$('#runit').click(function(){
		$('#hphrase').val(htmlentities($('#hphrase').val()));
	});
});
Ahhh shucks, this only does it to one field. So I'm assuming if I name all my inputs #hphrase it won't correct them all?
pcproff is offline   Reply With Quote
Old 09-22-2010, 10:33 PM   PM User | #12
harbingerOTV
Senior Coder

 
Join Date: Jan 2005
Location: Memphis, TN
Posts: 1,765
Thanks: 8
Thanked 123 Times in 121 Posts
harbingerOTV will become famous soon enough
killin me

Code:
$(document).ready(function(){
	$('#runit').click(function(){
		$('#myForm :input[type=text]').each(function(){
			$(this).val(htmlentities($(this).val()));
		});
	});
});
Code:
<form action="" method="" id="myForm">
<fieldset>
<label for="hphrase">Enter phrase:</label>
<input type="text" name="hphrase1" id="hphrase1" value="" tabindex="1" />
<input type="text" name="hphrase2" id="hphrase2" value="" tabindex="2" />
<input type="text" name="hphrase3" id="hphrase3" value="" tabindex="3" />
</fieldset>
<input type="button" value="Convert" tabindex="5" id="runit" />
</form>
__________________
Stop making things so hard on yourself.
i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

Last edited by harbingerOTV; 09-22-2010 at 10:48 PM.. Reason: added [type=text] to prevent default values from being changed on stuff like radios etc...
harbingerOTV is offline   Reply With Quote
Old 09-22-2010, 11:00 PM   PM User | #13
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
Cool

Quote:
Originally Posted by harbingerOTV View Post
killin me

Code:
$(document).ready(function(){
	$('#runit').click(function(){
		$('#myForm :input[type=text]').each(function(){
			$(this).val(htmlentities($(this).val()));
		});
	});
});
Code:
<form action="" method="" id="myForm">
<fieldset>
<label for="hphrase">Enter phrase:</label>
<input type="text" name="hphrase1" id="hphrase1" value="" tabindex="1" />
<input type="text" name="hphrase2" id="hphrase2" value="" tabindex="2" />
<input type="text" name="hphrase3" id="hphrase3" value="" tabindex="3" />
</fieldset>
<input type="button" value="Convert" tabindex="5" id="runit" />
</form>
Haha

This is funny because it is a form with a lot of inputs and if they hit convert again the "&quot;" gets turned into "&amp;quot;" because of the ampersand in front of the string. I'm thinking of making the button.visible=false but, next time they come to validate the form the script will see "&" and convert those as well. lol this is something else. I only need to replace quotes and double quotes which is the funny thing. I sent you a PM by the way.
pcproff is offline   Reply With Quote
Old 09-22-2010, 11:27 PM   PM User | #14
harbingerOTV
Senior Coder

 
Join Date: Jan 2005
Location: Memphis, TN
Posts: 1,765
Thanks: 8
Thanked 123 Times in 121 Posts
harbingerOTV will become famous soon enough
well if it's just those 2 characters you could just use this:
Code:
$(document).ready(function(){
	$('#runit').click(function(){
		$('#myForm :input[type=text]').each(function(){
			//$(this).val(htmlentities($(this).val()));
			$(this).val($(this).val().replace(/'/g,'''));
			$(this).val($(this).val().replace(/"/g,'&quot;'));
			
		});
	});
});
no need to call those other two big scripts.

if you only want it to happen once, like even if they push "convert" again it does nothing, you can remove the ID from the button like:

Code:
$(document).ready(function(){
	$('#runit').click(function(){
		$('#myForm :input[type=text]').each(function(){
			//$(this).val(htmlentities($(this).val()));
			$(this).val($(this).val().replace(/'/g,'''));
			$(this).val($(this).val().replace(/"/g,'&quot;'));
			
		});
	$(this).attr('id', '');
	});
});
but I am assuming this is so that the values are converted and then placed into a database so you could attach the click to a submit button like:

Code:
$(document).ready(function(){
	$('#myForm').submit(function(){
		$('#myForm :input[type=text]').each(function(){
			//$(this).val(htmlentities($(this).val()));
			$(this).val($(this).val().replace(/'/g,'''));
			$(this).val($(this).val().replace(/"/g,'&quot;'));
			
		});
	});
});
Code:
<form action="no.html" method="" id="myForm">
<fieldset>
<label for="hphrase">Enter phrase:</label>
<input type="text" name="hphrase" id="hphrase1" value="" tabindex="1" />
<input type="text" name="hphrase" id="hphrase2" value="" tabindex="2" />
<input type="text" name="hphrase" id="hphrase3" value="" tabindex="3" />
<input type="checkbox" value="apples & oranges" />stuff
</fieldset>
<input type="submit" value="Convert" tabindex="5" id="runit" />
</form>
that way the form gets submitted and they can't hit "convert" again.
__________________
Stop making things so hard on yourself.
i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis
harbingerOTV is offline   Reply With Quote
Users who have thanked harbingerOTV for this post:
pcproff (09-24-2010)
Old 09-23-2010, 11:23 PM   PM User | #15
pcproff
New Coder

 
Join Date: Nov 2006
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
pcproff is an unknown quantity at this point
You sir are the man! Thanks for all your help! Now to the book store to get a book on jquery.
pcproff is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:07 PM.


Advertisement
Log in to turn off these ads.