javascript check if number is hexadecimal 1

javascript check if number is hexadecimal

var re = /[0-9A-Fa-f]{6}/g;
var inputString = 'AABBCC';

if(re.test(inputString)) {
    alert('valid hex');
} else {
    alert('invalid');
}

re.lastIndex = 0; // be sure to reset the index after using .text()

Here is what the above code is Doing:
1. Create a regular expression object that matches a hexadecimal number.
2. Create a string to test against the regular expression.
3. Test the string against the regular expression.
4. If the string matches the regular expression, alert the user that it is a valid hexadecimal number.
5. If the string does not match the regular expression, alert the user that it is not a valid hexadecimal number.
6. Reset the regular expression object’s lastIndex property to 0.

Similar Posts