Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q348 How can I ensure that a string only contains characters that I want?

You are here: irt.org | FAQ | JavaScript | Text | Q348 [ previous next ]

Try something like this:

<SCRIPT language="JavaScript"><!--
function isValid(string,allowed) {
    for (var i=0; i< string.length; i++) {
       if (allowed.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}

var valid = '0123456789._'; // define valid characters

var test = '123';
alert(isValid(test,valid)); // returns true

var test = '1x2';
alert(isValid(test,valid)); // returns false
//--></SCRIPT>

Feedback on 'Q348 How can I ensure that a string only contains characters that I want?'

©2018 Martin Webb