widsnet.com
22Jul/140

Javascript to force inputbox to contain MAC

Don't know who invented this script in the first place. Wasn't me! But it's a really good script to have when having an input textbox that should contain a MAC address. Perhaps when importing a new computer in ConfigMgr through a website ...

<script type="text/javascript">
function updateColors()
{
	try
	{
		formatMAC('MAC');
	}
	catch(Error)
	{
	}
}

function allowedChar(theChar)
{
	var reject = false;
	switch (theChar)
	{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case 'a':
		case 'A':
		case 'b':
		case 'B':
		case 'c':
		case 'C':
		case 'd':
		case 'D':
		case 'e':
		case 'E':
		case 'f':
		case 'F':
		break;
		default:
		reject = true;
		break;
	}
	return reject == false? true:false;
}

function formatMAC(tbxID)
{
    var id = tbxID; 	
    var m_tbxMAC = document.getElementById(tbxID);
    if (m_tbxMAC == null)
    {
        var m_tmp = document.getElementsByTagName("input");
        var i = 0;
        for (i=0;i<=m_tmp.length;i++)
        {
            var pid = m_tmp&#91;i&#93;.id;         
            if (pid == null)
            {
            }
            else
            {
                var strsplit = pid.split("_");
                if (strsplit.length > 0)
                {
                    if (strsplit[strsplit.length-1] == tbxID)
                    {
                        id = pid;
                        m_tbxMAC = document.getElementById(pid);               
                        break;
                    }
                }
            }
        }
    }
    if (m_tbxMAC == null) return;

    m_tbxMAC.value =  m_tbxMAC.value.replace(/-/g,"");   
    var m_len = m_tbxMAC.value.length;

    var tmp = "";
    for (i=0;i<m_len;i++)
    {
        m_char = m_tbxMAC.value.substr(i,1);
        if (allowedChar(m_char) == true) tmp += m_char;
    }
    m_tbxMAC.value = tmp;
    m_len = m_tbxMAC.value.length;

    var B1 = '';
    var B2 = '';
    var B3 = '';
    var B4 = '';
    var B5 = '';
    var B6 = '';
    var B123456 = '';

    switch (m_len)
    {
        case 0:
            B123456 = '';
            break;
        case 1:  
        case 2:
            B123456 = m_tbxMAC.value;
            break;
        case 3:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,1);
            B123456 = B1 + ':' + B2;
            break;
        case 4:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B123456 = B1 + ':' + B2;
            break;
        case 5:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,1);
            B123456 = B1 + ':' + B2+ ':' + B3;
            break;
        case 6:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B123456 = B1 + ':' + B2+ ':' + B3;
            break;
        case 7:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B4 = m_tbxMAC.value.substr(6,1);
            B123456 = B1 + ':' + B2+ ':' + B3 + ':' + B4;
            break;
        case 8:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B4 = m_tbxMAC.value.substr(6,2);
            B123456 = B1 + ':' + B2+ ':' + B3 + ':' + B4;
            break;
        case 9:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B4 = m_tbxMAC.value.substr(6,2);
            B5 = m_tbxMAC.value.substr(8,1);
            B123456 = B1 + ':' + B2+ ':' + B3 + ':' + B4 + ':' + B5;
            break;
        case 10:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B4 = m_tbxMAC.value.substr(6,2);
            B5 = m_tbxMAC.value.substr(8,2);
            B123456 = B1 + ':' + B2+ ':' + B3 + ':' + B4 + ':' + B5;
            break;
        case 11: 
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B4 = m_tbxMAC.value.substr(6,2);
            B5 = m_tbxMAC.value.substr(8,2);
            B6 = m_tbxMAC.value.substr(10,1);
            B123456 = B1 + ':' + B2+ ':' + B3 + ':' + B4 + ':' + B5 + ':' + B6;
            break;
        case 12:
            B1 = m_tbxMAC.value.substr(0,2);
            B2 = m_tbxMAC.value.substr(2,2);
            B3 = m_tbxMAC.value.substr(4,2);
            B4 = m_tbxMAC.value.substr(6,2);
            B5 = m_tbxMAC.value.substr(8,2);
            B6 = m_tbxMAC.value.substr(10,2);
            B123456 = B1 + ':' + B2+ ':' + B3 + ':' + B4 + ':' + B5 + ':' + B6;
            break;

        default:
        B1 = m_tbxMAC.value.substr(0,2);
        B2 = m_tbxMAC.value.substr(2,2);
        B3 = m_tbxMAC.value.substr(4,2);
        B4 = m_tbxMAC.value.substr(6,2);
        B5 = m_tbxMAC.value.substr(8,2);
        B6 = m_tbxMAC.value.substr(10,2);
        B123456 = B1 + ':' + B2 + ':' + B3 + ':' + B4 + ':' + B5 + ':' + B6;
        break;
    }
    m_tbxMAC.value = B123456;
    /* Yellow color, not a full MAC but so far correct syntax and contents */
    if (m_tbxMAC.value.length != 17 &#038;& m_tbxMAC.value.length > 0) document.getElementById(id).style['backgroundColor'] = "#FFFF00";
	/* Green color, a full MAC value with correct syntax and contents */
	if (m_tbxMAC.value.length == 17) document.getElementById(id).style['backgroundColor'] = "#00FF00";
}
</script>
<input id="MAC" maxlength="17" name="MAC" size="20" type="text" value="<%=sMAC%>" />
Filed under: Javascript No Comments
22Jul/140

Javascript to check IP syntax

My first javascript. Colors the inputbox green if the syntax is correct. Simple check to test if the IP entered is "valid". Meaning an IP between 1.0.0.0 and 255.255.255.255.
Regexp pattern can easily be modified to check for some other known syntax. Computername or such.

<script type="text/javascript">
function ipCheck(formID) {
	var patt = /^([1-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])([.]([0-9]|[1-9][0-9]|[1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])){3}$/i;
	var form = document.getElementById(formID);
	if (patt.test(form.value)) {
		/* green if correct */
		form.style['backgroundColor'] = "#00FF00";
		/* form.value = form.value.toUpperCase(); 
		I used this for another function to check for the computername syntax 
		and force it to be uppercase when entered correct. */
	} else if (form.value == "") {
		/* Remove color if blank value */
		form.style['backgroundColor'] = "";
	} else {
		/* yellow otherwise */
		form.style['backgroundColor'] = "#FFFF00";
	}
}
</script>
<input type="text" name="Subnet" id="Subnet" Value="<%=sSubnet%>" Maxlength="15" onmouseout="ipCheck('Subnet')" onkeyup="ipCheck('Subnet')">
Filed under: Javascript No Comments