I'm trying to validate certain alphanumeric characters within javascript and all works but a colon (:). My code is:

if (![\w_:.=\(\)@\-\/]/.test(field))
    return false;

I've placed a \ in front of the : and moved the colon to other positions, tried it in its own brackets [:] but it just won't accept a colon.

You would need to elaborate the "certain alphanumeric characters" or I am not sure what you mean by that. If you are looking for number only (which could be both decimal and float), you could use the built-in function isNaN() which validate whether the incoming argument is not-a-number.

//i.e.
isNaN("abcd")  // <== true
isNaN("2355")  // <== false
isNaN("2355.44")  // <== false
isNaN("+2355.44")  // <== false
isNaN("++2355.44")  // <== true
isNaN("-2355.41")  // <== false
isNaN(-2355.41)  // <== false
isNaN("--2355")  // <== true

Unless your meaning of "certain alphanumeric charaters" is different, the function should work fine.

Perhaps I should have been more specific. I'm attempting to validate fields that can include alphanumeric and certain special characters. A specific example: http://us2.mailchaimp.com/folder/document.doc The javascript code accepts all but the colon after "http"even though it is included in the .test parameters.

OK, how about using regex instead?

// i.e.
// regex is inside 2 forward slashes.
// \w --> a-zA-Z0-9_
// :  --> colon
// \. --> a period
// =  --> an equal sign
// \( --> open parenthesis
// \) --> close parenthesis
// @  --> at sign
// \/ --> forward slash
// -  --> hyphen
function chk (str) {
 if(str=="undefined" || str==null) { return false; }
 if (str.match(/[\w_:\.=\(\)@\/-]+/)) { return true; }
 return false;
}

I tried it and it still doesn't allow a colon to be added to the textbox. I've moved the colon around in the expression, used \: to imply metacharacter, tried .match versus .test... For whatever reason, it allows all the other special characters but not a colon. I wonder if there is a javascript problem. I'm using xampp v3.2.1. The browser is IE 11. This used to work fine before I upgraded to Windows 8.1.

Hmm... Interesting. It works on FF and Chrome. I will have to dig a bit on IE 11...

By the way, you are trying to sanitize the string using observe instead of onchange or onsubmit?

I'm using onchange but I just overcame the problem. There must something wierd going on in javascript. I changed the sequence of the characters to [:\w etc. and it now works. I'm guessing there is either a character scanning problem in either javascript or html. In any event, it now works. Thanks for your help Taywin.

ok, let's try something... Below is a simple HTML. When you enter anything inside the text area and click the 'Sanitize' button, it should remove anything else but whatever characters you allowed. Let see if it allow colon character in the result?

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript">
  function sanitizeTextArea(elemId) {
    var el = document.getElementById(elemId);
    if (el) { el.value = sanitizeStr(el.value); }
  }
  function sanitizeStr(str) {
    if (str==null) { return ""; }
    return str.replace(/[^\w\(\)@:=-]/g, "");  // remove anything not match
  }
  </script>
  <title>Check</title>
</head>

<body>
  <textarea id="inputText" rows="4" cols="60"></textarea>
  <br>
  <input type="button" onclick="sanitizeTextArea('inputText')" value="Sanitize">
</body>
</html>

Edit: OK, good then. I don't know much about IE11 because I don't have a Window8 box around... :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.