nix, shell, perl, php, mysql and mac os x tips and tricks

Tuesday, December 5, 2006

Use javascript to search-and-replace international characters in form entry fields (and do some other stuff)

 function ak_hi_check() {

    if  ( (mainForm.shipping_state.value == "AK") && (mainForm.ShipVia.value != "25") ) {
        alert("You must choose Alaska/Hawaii as your shipping method");
        return false;
    } 
   
    else if  ( (mainForm.shipping_state.value == "HI") && (mainForm.ShipVia.value != "25") ) {
        alert("You must choose Alaska/Hawaii as your shipping method");
        return false;
    } 
   
    else if  ( (mainForm.shipping_state.value == "PR") && (mainForm.ShipVia.value != "27") ) {
        alert("You must choose Puerto Rico as your shipping method");
        return false;
    } 
  
else {
        return true;
    }
}

// searches and replaces intl characters
function mySubmit()
 {
     //replace any special characters entered into text entry fields
    document.mainForm.billing_name.value = convert(document.mainForm.billing_name.value);
    document.mainForm.billing_first_name.value = convert(document.mainForm.billing_first_name.value);
    document.mainForm.billing_last_name.value = convert(document.mainForm.billing_last_name.value);
    document.mainForm.billing_addr1.value = convert(document.mainForm.billing_addr1.value);
    document.mainForm.billing_addr2.value = convert(document.mainForm.billing_addr2.value);
    document.mainForm.billing_city.value = convert(document.mainForm.billing_city.value);
    document.mainForm.billing_zip.value = convert(document.mainForm.billing_zip.value);
    document.mainForm.billing_home_phone.value = convert(document.mainForm.billing_home_phone.value);
    document.mainForm.shipping_name.value = convert(document.mainForm.shipping_name.value);
    document.mainForm.shipping_first_name.value = convert(document.mainForm.shipping_first_name.value);
    document.mainForm.shipping_last_name.value = convert(document.mainForm.shipping_last_name.value);
    document.mainForm.shipping_addr1.value = convert(document.mainForm.shipping_addr1.value);
    document.mainForm.shipping_addr2.value = convert(document.mainForm.shipping_addr2.value);
    document.mainForm.shipping_city.value = convert(document.mainForm.shipping_city.value);
    document.mainForm.shipping_zip.value = convert(document.mainForm.shipping_zip.value);
    document.mainForm.shipping_home_phone.value = convert(document.mainForm.shipping_home_phone.value);
    document.mainForm.po_number.value = convert(document.mainForm.po_number.value);
   
    // make sure they are choosing the correct ship method
    if  (ak_hi_check()==false) {
           return false;
    } 
    else {
        document.mainForm.action = "InvoicePreview";
        document.mainForm.submit();
    }
}

// the characters to search-and-replace
function convert(text)
{
    text = text.replace(/À/gi,"a");
    text = text.replace(/Á/gi,"a");
    text = text.replace(/Â/gi,"a");
    text = text.replace(/Ã/gi,"a");
    text = text.replace(/Ä/gi,"a");
    text = text.replace(/Å/gi,"a");
    text = text.replace(/Æ/gi,"ae");
    text = text.replace(/Ç/gi,"c");
    text = text.replace(/È/gi,"e");
    text = text.replace(/É/gi,"e");
    text = text.replace(/Ê/gi,"e");
    text = text.replace(/Ë/gi,"e");
    text = text.replace(/Ì/gi,"i");
    text = text.replace(/Í/gi,"i");
    text = text.replace(/Î/gi,"i");
    text = text.replace(/Ï/gi,"i");
    text = text.replace(/Ð/gi,"eth");
    text = text.replace(/Ñ/gi,"n");
    text = text.replace(/Ò/gi,"o");
    text = text.replace(/Ó/gi,"o");
    text = text.replace(/Ô/gi,"o");
    text = text.replace(/Õ/gi,"o");
    text = text.replace(/Ö/gi,"o");
    text = text.replace(/Ø/gi,"o");
    text = text.replace(/Ù/gi,"u");
    text = text.replace(/Ú/gi,"u");
    text = text.replace(/Û/gi,"u");
    text = text.replace(/Ü/gi,"u");
    text = text.replace(/Ý/gi,"y");
    text = text.replace(/Þ/gi,"THORN");
    text = text.replace(/ß/gi,"s");
    text = text.replace(/à/gi,"a");
    text = text.replace(/á/gi,"a");
    text = text.replace(/â/gi,"a");
    text = text.replace(/ã/gi,"a");
    text = text.replace(/ä/gi,"a");
    text = text.replace(/å/gi,"a");
    text = text.replace(/æ/gi,"ae");
    text = text.replace(/ç/gi,"c");
    text = text.replace(/è/gi,"e");
    text = text.replace(/é/gi,"e");
    text = text.replace(/ê/gi,"e");
    text = text.replace(/ë/gi,"e");
    text = text.replace(/ì/gi,"i");
    text = text.replace(/í/gi,"i");
    text = text.replace(/î/gi,"i");
    text = text.replace(/ï/gi,"i");
    text = text.replace(/ð/gi,"eth");
    text = text.replace(/ñ/gi,"n");
    text = text.replace(/ò/gi,"o");
    text = text.replace(/ó/gi,"o");
    text = text.replace(/ô/gi,"o");
    text = text.replace(/õ/gi,"o");
    text = text.replace(/ö/gi,"o");
    text = text.replace(/ø/gi,"o");
    text = text.replace(/ù/gi,"u");
    text = text.replace(/ú/gi,"u");
    text = text.replace(/û/gi,"u");
    text = text.replace(/ü/gi,"u");
    text = text.replace(/ý/gi,"y");
    text = text.replace(/þ/gi,"thorn");
    text = text.replace(/ÿ/gi,"y");
    return text;
}

No comments:

Post a Comment