views:

136

answers:

1

I have a function that contains messages for form field validation. However I would like to have default messages but also have the possibility to add custom messages later on from a different function (on the fly). I have tried something using php and extending a class and adding more messages and than building the javascript but I'm not really happy doing it this way.

function msg_text(fieldV){

msg = {
    "required":{          
        "alertText":"* This field is required"
    },
    "length":{
        "alertText":"* minimum 6 characters "
    },
    "numeric":{
        "alertText":"numbers and * only<br />minimum 3 characters"
    },
    "email":{
        "alertText":"* Invalid email address"
    },
    "no_space":{
        "alertText":"* Is Required <br /> * Space not allowed"
    }
}

return msg[fieldV].alertText;  //returns alert message

}

A: 
    var my{};

function createFunctions()
{
   var msg = {/*messages*/};


    my.getMessage = function(key)
    {
         return msg[key];
    }

    my.addMessage = function(key, message)
    {
         msg[key] = message
    }

    my.deleteMessage = function(key)
    {
        delete msg[key];
    }
}
Trickster
Thank you for your answer. my.addMessage doesn't seem to work if the key does not exist in var msg. However, it works and replaces the message if the same key exist in var msg. I have tried this: msg[key].alertText = message;
Chris
I have figured it out. This code works for me:msg[key] = {"alertText":message};
Chris