开发者

Javascript extend objects in variable

开发者 https://www.devze.com 2022-12-13 16:02 出处:网络
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 functio

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 javasc开发者_Go百科ript 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

}


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];
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消