1

I'm working on a validation script, but I'm running into a very particular issue.

If a user enters a string that happens to be an encoded html character (like & or &), it will output as the character (& in this case). My question is this: is it possible to write a function that detemines if a string is an encoded character? So if the user enters one of the two above options, I want to launch a particular function, and if it's a non-encoded character, I want to do something else.

Is there a way to do this?

3
  • Are you trying to test just for those two specific examples, or for any html character entities? If it's just those two a simple regex should do it: if (/&|&/.test(strVar)) { ... } Commented Dec 4, 2012 at 12:52
  • @streetlight- you can onkeypress() and check for ( & or &) It will run goood.! Commented Dec 4, 2012 at 12:53
  • I'm am trying to do all html character entries, not just these specific two. Thank you though! Commented Dec 4, 2012 at 12:56

3 Answers 3

3

By definition, if you do not know whether something is an encoded HTML entity or not you do not know. Either you treat all text coming from a certain source as encoded or not encoded. Why? Because it's all just text. "&" is just text. I meant to write "&" here. I do not want anyone to interpret it, I want it to appear literally as "&".

How do you know what the user meant? If you're starting to replace user-entered text based on guesses, you'll always screw it up in some cases. It's the typical case where all ":D" is replaced by a graphical smilie, which is annoying when you actually wanted to type ":D".

If you want to always preserve exactly what the user entered, always run all user input through an HTML-encoding function which replaces all special characters with entities. See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).

Sign up to request clarification or add additional context in comments.

Comments

1

You can check if a string contains encoded characters by comparing the encoded vs decoded lengths:

var string = "Your encoded & decoded string here"

function decode(str){
    return decodeURIComponent(str).replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}

if(string.length == decode(string).length){
    // The string does not contain any encoded html.
}else{
    // The string contains encoded html.
}

Also, this is significantly faster than the jQuery method that was suggested.

Comments

0

Something like this would do it.

function containsEncoded (val){
    var rHTMLEncoded = /&[^\s]*/;

    return rHTMLEncoded.test(val) ;
}


// Usage 
var encoded = containsEncoded("&amp;");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.