script src="../js/Mask.js" type="text/javascript"
Mask.js
// Mask for Phone numbers and Zipcode
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var maxFEINlength = 10;
var maxZipcodelength = 10;
var maxSSNlength = 11;
var phonevalue1;
var phonevalue2;
var cursorposition;
function ParseForNumber1(object) {
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object) {
phonevalue2 = ParseChar(object.value, zChar);
}
function backspacerUPSSN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidateSSN(object)
}
}
function backspacerUPPhone(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidatePhone(object)
}
}
function backspacerUPFEIN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidateFEIN(object)
}
}
function backspacerUPZipcode(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidateZipcode(object)
}
}
function backspacerDOWNSSN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function backspacerDOWNPhone(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function backspacerDOWNFEIN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function backspacerDOWNZipcode(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function GetCursorPosition() {
var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i = 0; i < t1.length; i++) {
if (t1.substring(i, 1) != t2.substring(i, 1)) {
if (!bool) {
cursorposition = i
window.status = cursorposition
bool = true
}
}
}
}
function ValidateFEIN(object) {
var p = phonevalue1
if (p.length < 2) {
object.value = p
} else if (p.length >= 9) {
p = p;
l30 = p.length;
p30 = p.substring(0, 2);
p30 = p30 + "-"
p31 = p.substring(2, p.length);
pp = p30 + p31;
object.value = pp.substring(0, maxFEINlength);
}
}
function ValidateZipcode(object) {
var p = phonevalue1
if (p.length < 5) {
object.value = p
} else if (p.length >= 10) {
p = p;
l30 = p.length;
p30 = p.substring(0, 5);
p30 = p30 + "-"
p31 = p.substring(5, p.length);
pp = p30 + p31;
object.value = pp.substring(0, maxZipcodelength);
}
}
function ValidateSSN(object) {
var p = phonevalue1
if (p.length < 5) {
object.value = p
} else if (p.length >= 10) {
p = p;
l30 = p.length;
p30 = p.substring(0, 3);
p30 = p30 + "-"
p31 = p.substring(0, 2);
p31 = p31 + "-"
p32 = p.substring(0, 6);
pp = p30 + p31 + p32;
object.value = pp.substring(0, maxSSNlength);
}
}
function ValidatePhone(object) {
var p = phonevalue1
p = p.replace(/[^\d]*/gi, "")
if (p.length < 3) {
object.value = p
} else if (p.length == 3) {
pp = p;
d4 = p.indexOf('(')
d5 = p.indexOf(')')
if (d4 == -1) {
pp = "(" + pp;
}
if (d5 == -1) {
pp = pp + ")";
}
object.value = pp;
} else if (p.length > 3 && p.length < 7) {
p = "(" + p;
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ") "
p31 = p.substring(4, l30);
pp = p30 + p31;
object.value = pp;
} else if (p.length >= 7) {
p = "(" + p;
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ") "
p31 = p.substring(4, l30);
pp = p30 + p31;
l40 = pp.length;
p40 = pp.substring(0, 9);
p40 = p40 + "-"
p41 = pp.substring(9, l40);
ppp = p40 + p41;
object.value = ppp.substring(0, maxphonelength);
}
GetCursorPosition()
if (cursorposition >= 0) {
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 4) {
cursorposition = cursorposition + 3
} else if (cursorposition == 5) {
cursorposition = cursorposition + 3
} else if (cursorposition == 6) {
cursorposition = cursorposition + 3
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
} else if (cursorposition == 8) {
cursorposition = cursorposition + 4
e1 = object.value.indexOf(')')
e2 = object.value.indexOf('-')
if (e1 > -1 && e2 > -1) {
if (e2 - e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition == 9) {
cursorposition = cursorposition + 4
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition == 12) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 13) {
cursorposition = cursorposition
}
var txtRange = object.createTextRange();
txtRange.moveStart("character", cursorposition);
txtRange.moveEnd("character", cursorposition - object.value.length);
txtRange.select();
}
}
function ParseChar(sStr, sChar) {
if (sChar.length == null) {
zChar = new Array(sChar);
}
else zChar = sChar;
for (i = 0; i < zChar.length; i++) {
sNewStr = "";
var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);
while (iEnd != -1) {
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
sStr = sNewStr;
}
return sNewStr;
}
// Remove Specail Charactes like $,',.,etc
function CheckRatesandSPCharacters(object)
{
var temp = object.value;
temp = temp.replace(/[^a-zA-Z 0-9.]+/g, '');
var result = Math.round(temp);
object.value = result;
}
function CheckRatesandSPCharacters1(object) {
var temp = object.value;
temp = temp.replace(/[^a-zA-Z 0-9.]+/g, '');
var temp2 = Math.round(temp);
var result = '$' + temp2;
object.value = result;
}
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// Also separates date into month, day, and year variables
function isValidDate(object) {
var dateStr = object.value;
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
object.value = "";
object.focus();
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
object.value = "";
object.focus();
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
object.value = "";
object.focus();
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
alert("Month " + month + " doesn't have 31 days!")
object.value = "";
object.focus();
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day == 29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
object.value = "";
object.focus();
return false;
}
}
return true; // date is valid
}
function PhoneNumberFormat(formfield) {
var currentValue = new String(formfield.value);
var id = formfield.id;
var currentStrippedValue = ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(currentValue, "-", ""), " ", ""), "(", ""), ")", ""); //strip the dashes or parentheses from phone number
lastField = id;
if (currentStrippedValue.length > 0) {
if (isNaN(currentStrippedValue)) {
alert("Please Enter Numeric Values Only (###)###-####"); //Make sure only numbers are entered
formfield.value = "";
formfield.select();
lastFieldIsValid = false;
return;
} else {
if (currentStrippedValue.length != 10) //Verify that the area code and telephone number are entered
{
alert("Phone # Must Be 10 Numbers Long (###)###-####");
formfield.select();
lastFieldIsValid = false;
return;
}
else {
lastFieldIsValid = true;
formfield.value = "(" + currentStrippedValue.substring(0, 3) + ") " + currentStrippedValue.substring(3, 6) + "-" + currentStrippedValue.substring(6, 10); //Format the string for correct display
}
}
}
}
function ReplaceAll(checkMe, toberep, repwith) {
var temp = checkMe;
var i = temp.indexOf(toberep);
while (i > -1) { //Loop through and replace all instances
temp = temp.replace(toberep, repwith);
i = temp.indexOf(toberep);
}
return temp;
}
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Pavel Donchev | http://chameleonbulgaria.com/ */
function currency(which) {
currencyValue = which.value;
currencyValue = currencyValue.replace(",", "");
decimalPos = currencyValue.lastIndexOf(".");
if (decimalPos != -1) {
decimalPos = decimalPos + 1;
}
if (decimalPos != -1) {
decimal = currencyValue.substring(decimalPos, currencyValue.length);
if (decimal.length > 2) {
decimal = decimal.substring(0, 2);
}
if (decimal.length < 2) {
while (decimal.length < 2) {
decimal += "0";
}
}
}
if (decimalPos != -1) {
fullPart = currencyValue.substring(0, decimalPos - 1);
} else {
fullPart = currencyValue;
decimal = "00";
}
newStr = "";
for (i = 0; i < fullPart.length; i++) {
newStr = fullPart.substring(fullPart.length - i - 1, fullPart.length - i) + newStr;
if (((i + 1) % 3 == 0) & ((i + 1) > 0)) {
if ((i + 1) < fullPart.length) {
newStr = "," + newStr;
}
}
}
which.value = newStr + "." + decimal;
}
function normalize(which) {
alert("Normal");
val = which.value;
val = val.replace(",", "");
which.value = val;
}
Mask.js
// Mask for Phone numbers and Zipcode
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var maxFEINlength = 10;
var maxZipcodelength = 10;
var maxSSNlength = 11;
var phonevalue1;
var phonevalue2;
var cursorposition;
function ParseForNumber1(object) {
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object) {
phonevalue2 = ParseChar(object.value, zChar);
}
function backspacerUPSSN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidateSSN(object)
}
}
function backspacerUPPhone(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidatePhone(object)
}
}
function backspacerUPFEIN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidateFEIN(object)
}
}
function backspacerUPZipcode(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber1(object)
if (keycode >= 48) {
ValidateZipcode(object)
}
}
function backspacerDOWNSSN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function backspacerDOWNPhone(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function backspacerDOWNFEIN(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function backspacerDOWNZipcode(object, e) {
if (e) {
e = e
} else {
e = window.event
}
if (e.which) {
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}
function GetCursorPosition() {
var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i = 0; i < t1.length; i++) {
if (t1.substring(i, 1) != t2.substring(i, 1)) {
if (!bool) {
cursorposition = i
window.status = cursorposition
bool = true
}
}
}
}
function ValidateFEIN(object) {
var p = phonevalue1
if (p.length < 2) {
object.value = p
} else if (p.length >= 9) {
p = p;
l30 = p.length;
p30 = p.substring(0, 2);
p30 = p30 + "-"
p31 = p.substring(2, p.length);
pp = p30 + p31;
object.value = pp.substring(0, maxFEINlength);
}
}
function ValidateZipcode(object) {
var p = phonevalue1
if (p.length < 5) {
object.value = p
} else if (p.length >= 10) {
p = p;
l30 = p.length;
p30 = p.substring(0, 5);
p30 = p30 + "-"
p31 = p.substring(5, p.length);
pp = p30 + p31;
object.value = pp.substring(0, maxZipcodelength);
}
}
function ValidateSSN(object) {
var p = phonevalue1
if (p.length < 5) {
object.value = p
} else if (p.length >= 10) {
p = p;
l30 = p.length;
p30 = p.substring(0, 3);
p30 = p30 + "-"
p31 = p.substring(0, 2);
p31 = p31 + "-"
p32 = p.substring(0, 6);
pp = p30 + p31 + p32;
object.value = pp.substring(0, maxSSNlength);
}
}
function ValidatePhone(object) {
var p = phonevalue1
p = p.replace(/[^\d]*/gi, "")
if (p.length < 3) {
object.value = p
} else if (p.length == 3) {
pp = p;
d4 = p.indexOf('(')
d5 = p.indexOf(')')
if (d4 == -1) {
pp = "(" + pp;
}
if (d5 == -1) {
pp = pp + ")";
}
object.value = pp;
} else if (p.length > 3 && p.length < 7) {
p = "(" + p;
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ") "
p31 = p.substring(4, l30);
pp = p30 + p31;
object.value = pp;
} else if (p.length >= 7) {
p = "(" + p;
l30 = p.length;
p30 = p.substring(0, 4);
p30 = p30 + ") "
p31 = p.substring(4, l30);
pp = p30 + p31;
l40 = pp.length;
p40 = pp.substring(0, 9);
p40 = p40 + "-"
p41 = pp.substring(9, l40);
ppp = p40 + p41;
object.value = ppp.substring(0, maxphonelength);
}
GetCursorPosition()
if (cursorposition >= 0) {
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 4) {
cursorposition = cursorposition + 3
} else if (cursorposition == 5) {
cursorposition = cursorposition + 3
} else if (cursorposition == 6) {
cursorposition = cursorposition + 3
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
} else if (cursorposition == 8) {
cursorposition = cursorposition + 4
e1 = object.value.indexOf(')')
e2 = object.value.indexOf('-')
if (e1 > -1 && e2 > -1) {
if (e2 - e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition == 9) {
cursorposition = cursorposition + 4
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition == 12) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 13) {
cursorposition = cursorposition
}
var txtRange = object.createTextRange();
txtRange.moveStart("character", cursorposition);
txtRange.moveEnd("character", cursorposition - object.value.length);
txtRange.select();
}
}
function ParseChar(sStr, sChar) {
if (sChar.length == null) {
zChar = new Array(sChar);
}
else zChar = sChar;
for (i = 0; i < zChar.length; i++) {
sNewStr = "";
var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);
while (iEnd != -1) {
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
sStr = sNewStr;
}
return sNewStr;
}
// Remove Specail Charactes like $,',.,etc
function CheckRatesandSPCharacters(object)
{
var temp = object.value;
temp = temp.replace(/[^a-zA-Z 0-9.]+/g, '');
var result = Math.round(temp);
object.value = result;
}
function CheckRatesandSPCharacters1(object) {
var temp = object.value;
temp = temp.replace(/[^a-zA-Z 0-9.]+/g, '');
var temp2 = Math.round(temp);
var result = '$' + temp2;
object.value = result;
}
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// Also separates date into month, day, and year variables
function isValidDate(object) {
var dateStr = object.value;
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
object.value = "";
object.focus();
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
object.value = "";
object.focus();
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
object.value = "";
object.focus();
return false;
}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
alert("Month " + month + " doesn't have 31 days!")
object.value = "";
object.focus();
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day == 29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
object.value = "";
object.focus();
return false;
}
}
return true; // date is valid
}
function PhoneNumberFormat(formfield) {
var currentValue = new String(formfield.value);
var id = formfield.id;
var currentStrippedValue = ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(currentValue, "-", ""), " ", ""), "(", ""), ")", ""); //strip the dashes or parentheses from phone number
lastField = id;
if (currentStrippedValue.length > 0) {
if (isNaN(currentStrippedValue)) {
alert("Please Enter Numeric Values Only (###)###-####"); //Make sure only numbers are entered
formfield.value = "";
formfield.select();
lastFieldIsValid = false;
return;
} else {
if (currentStrippedValue.length != 10) //Verify that the area code and telephone number are entered
{
alert("Phone # Must Be 10 Numbers Long (###)###-####");
formfield.select();
lastFieldIsValid = false;
return;
}
else {
lastFieldIsValid = true;
formfield.value = "(" + currentStrippedValue.substring(0, 3) + ") " + currentStrippedValue.substring(3, 6) + "-" + currentStrippedValue.substring(6, 10); //Format the string for correct display
}
}
}
}
function ReplaceAll(checkMe, toberep, repwith) {
var temp = checkMe;
var i = temp.indexOf(toberep);
while (i > -1) { //Loop through and replace all instances
temp = temp.replace(toberep, repwith);
i = temp.indexOf(toberep);
}
return temp;
}
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Pavel Donchev | http://chameleonbulgaria.com/ */
function currency(which) {
currencyValue = which.value;
currencyValue = currencyValue.replace(",", "");
decimalPos = currencyValue.lastIndexOf(".");
if (decimalPos != -1) {
decimalPos = decimalPos + 1;
}
if (decimalPos != -1) {
decimal = currencyValue.substring(decimalPos, currencyValue.length);
if (decimal.length > 2) {
decimal = decimal.substring(0, 2);
}
if (decimal.length < 2) {
while (decimal.length < 2) {
decimal += "0";
}
}
}
if (decimalPos != -1) {
fullPart = currencyValue.substring(0, decimalPos - 1);
} else {
fullPart = currencyValue;
decimal = "00";
}
newStr = "";
for (i = 0; i < fullPart.length; i++) {
newStr = fullPart.substring(fullPart.length - i - 1, fullPart.length - i) + newStr;
if (((i + 1) % 3 == 0) & ((i + 1) > 0)) {
if ((i + 1) < fullPart.length) {
newStr = "," + newStr;
}
}
}
which.value = newStr + "." + decimal;
}
function normalize(which) {
alert("Normal");
val = which.value;
val = val.replace(",", "");
which.value = val;
}
No comments:
Post a Comment