Jump to content



Photo

HTML/JS encoding question

html javascript

  • Please log in to reply
3 replies to this topic

#1 eliokh

eliokh

    Neowinian

  • Joined: 01-July 10
  • Location: Lebanon
  • OS: Win7/XP

Posted 19 November 2012 - 09:31

Hello,

I need to ask a small question about character encoding in HTML.

When submitting a form, i am doing a client side validation that will read all data from the form and "Escape" it in a way to transform < into &gt; single quotes to &#39; etc .




function verufymyform(form){
  
  
  var str = ''; 
  var elem = form.elements;
  for(var i = 0; i < elem.length; i++)
  {
str = characterEncodingFunction(elem[i].value); //here is the function i am calling
    elem[i].value = str;  //I think i should add a JS function here??[/size]
[size=3]  }	  
  return false;
}[/size]
[size=3]



now the problem arises when i have for example a submit button with value "J'accept". When clicking it , its value becomes J&#39;accepte.

What function in javascript should i add so the display is not modified?


thank you in advance.



#2 OP eliokh

eliokh

    Neowinian

  • Joined: 01-July 10
  • Location: Lebanon
  • OS: Win7/XP

Posted 19 November 2012 - 10:16

anybody??

#3 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 27
  • Joined: 23-January 11
  • Location: Netherlands
  • OS: Windows 8.1 Pro
  • Phone: Nexus 5

Posted 19 November 2012 - 16:14

so your js applies to everything inside the 'form' element but you want to make a exception of the submit button?
you can try:
<form>
	<div id="form">
	   <input blabla />
	   <input blabla />
	   <input blabla />
	</div>
	<input type="submit" value="J'accept" />
</form>
function verufymyform(form){
  
  
  var str = '';
  var elem = document.getElementById('form').elements;
  for(var i = 0; i < elem.length; i++)
  {
str = characterEncodingFunction(elem[i].value); //here is the function i am calling
	elem[i].value = str;  //I think i should add a JS function here??
  }	  
  return false;
}

This should work :p

#4 -Alex-

-Alex-

    Noob Hunter

  • Joined: 08-August 06
  • Location: Oslo, Norway

Posted 28 November 2012 - 04:05

That would work. To avoid having another div, and to allow the parameter of the JavaScript function to actually continue to do something, this should work:

function verufymyform(form){
  var elem = form.elements;
  for(var i = 0; i < elem.length; i++)
  {
    if(elem[i].type != "sumbit")
      elem[i].value = characterEncodingFunction(elem[i].value);
  }
  return false;
}

I also removed the references to str since this was unnecessary.