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

Q618 How do I validate that a number entered in a text field is a hexidecimal number?

You are here: irt.org | FAQ | JavaScript | Number | Q618 [ previous next ]

Try:

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

var valid = '0123456789ABCDEFabcdef'; // define valid characters

//--></SCRIPT>

<FORM NAME="myForm" onSubmit="return isValid(document.myForm.input.value,valid)">
<INPUT TYPE="TEXT" NAME="input">
<INPUT TYPE="SUBMIT" VALUE="Valid?">
</FORM>

©2018 Martin Webb