我有一个包含表单字段验证消息的函数。不过,我希望有默认消息,但也可以稍后从不同的函数(即时)添加自定义消息。我尝试过使用 php 并扩展一个类并添加更多消息,而不是构建 javascript,但我不太高兴这样做。
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];
}
}
关于Javascript 扩展变量中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887212/