javascript question - validate mac address
I am trying to validate a mac address that has been keyed by a user. I am not exactly sure how to convert a character to a digit to compare to an ascii character code or how to convert the character to hexadecimal. Either would work well, but every method I have tried has failed. :mad: Below is the code that I am using as well as some scratch work.
function validateMacAddress(oFormObj) {
myMac = String(oFormObj.value);
myMac = myMac.toUpperCase();
oFormObj.value = myMac;
if (myMac.length != 12) {
alert('Length failure!');
return false;
}
for(i=0; i < 12; i++) {
myChar = myMac.charAt(i);
alert(myChar);
myDigit = Character.digit(myChar, 10);
alert(myDigit);
/*if (Character.digit(myChar, 16) > 16) {
alert ("Character Failure");
}
*/
}
}
[880 byte] By [
fizch] at [2007-11-18 5:44:37]

# 1 Re: javascript question - validate mac address
I am trying to validate a mac address that has been keyed by a user.
Are you trying to validate the mac address for a particular pattern?
I dont understand the problem or what your code is doing
Could you elaborate?
:confused:
Satish
# 3 Re: javascript question - validate mac address
OK, now I get it.
The cleanest and most efficient solution to this would be to use Javascript regular expressions. That way you can write a script that validates the values entered to 0-9 and A-F
I dont have an example right now, but a search on the Internet should give you hundreds of samples.
Satish
# 4 Re: javascript question - validate mac address
I am not having much luck using the Regular Expression. Here is what I have tried.
reHex = new RegExp("0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f", "gi")
if (!myMac.search(reHex)) {
alert("Hex Failed");
return false;
}
I am probably using the wrong type of regular expression, but I am rather unfamiliar with the concept.
fizch at 2007-11-8 0:16:55 >

# 5 Re: javascript question - validate mac address
This should work
var regex="";
var teststr="";
function mactest()
{
regex=/(\d|[a-f]){12}/;
teststr=document.frm1.txt1.value;
if (regex.test(teststr))
{
alert("Valid mac address");
}
else
{
alert("Not a valid mac address");
}
}
where document.frm1.txt1 is the form field where the value is entered.
Satish
# 6 Re: javascript question - validate mac address
Actually, that did not work for me. However, I have changed the logic a little and done my validation with a nice long If statement.
fizch at 2007-11-8 0:19:03 >
