/**
* checkAllInput() 校验页面中所有标识的输入框信息 使用场景 在点击提交按钮的时候调用该方法对页面中所有标识为 mstype="1" 的做非空校验和格式校验。
*
* msginfo(objid,objmess) 校验非空信息提示 #B0B0B0 灰色色值 其它组建提示信息可直接使用表示提示类
* errinfo(objid,objmess) 校验格式错误信息提示 RED 红色色值 其它组建提示信息可直接使用表示error类
*
* 给引用页面动态添加验证事件
* 案例:<div class="col-sm-8">
* <label>及格分数</label> <input type="text" id="PassScore" >
* <span>分</span>
* </div>
* 例如:要给PassScore 添加校验是否为null 则添加 mstype="1" placeholder="及格分数" 还需要校验这个分数是否是包含小数的 name再添加一个属性 fmt="float"
* 注释:input id 为必有项, mstype="1":标识该输入框为需要校验 placeholder="及格分数" placeholder:标识这个文本框需要输入的内容的名称 fmt="float" 是需要校验的格式
* fmt 标识校验数据类型的格式,目前支持 int 整型 ; float: 小数; mobile:手机号格式校验;email:格式校验;code 校验是否是英文开头字母和数字结合的字符串
* 前台展示后代码为 输入 wsd122 name在文本框失去焦点的时候就会出现提示信息。
* <div class="col-sm-8">
* <label>及格分数</label> <input type="text" id="PassScore" mstype="1" placeholder="及格分数" fmt="float">
* <span>分</span>
* <div name='error'><span style='color: Red;font-size: 12px;'><i class='fa fa-info-circle'></i>及格分数格式不正确</span></div>
* </div>
*/
//动态提示请输入信息
function msginfo(objid, objmess) {
if ($("#" + objid).parent().children("div:last-child") == undefined || $("#" + objid).parent().children("div:last-child").attr("name") != "error") {
$("#" + objid).parent().append("<div name='error'><span style='color: #B0B0B0;font-size: 12px;'><i class='fa fa-info-circle'></i>" + objmess + "</span></div>");
}
}
//校验格式的数据
function errinfo(objid, objmess) {
if ($("#" + objid).parent().children("div:last-child") == undefined ||$("#" + objid).parent().children("div:last-child").attr("name") != "error") {
$("#" + objid).parent().append("<div name='error'><span style='color: Red;font-size: 12px;'><i class='fa fa-info-circle'></i>" + objmess + "</span></div>");
}
}
//初始化检验信息
$(function() {
$("input[mstype=1]").blur(function() {
if ($(this).attr("mstype") == 1) {
return checkformat($(this).attr("id"));
}
});
})
//校验代码部分
function checkformat(objid) {
var flag = true;
var fmt=$("#" + objid).attr("fmt");
var value=$("#" + objid).val();
var placeholder=$("#" + objid).attr("placeholder");
if ($("#" + objid).parent().children("div:last-child").attr("name") == "error")
{
$("#" + objid).parent().children("div:last-child").remove();
}
if(isEmpty(value)){
msginfo(objid, "请输入"+placeholder );
flag = false;
}
if(flag){
switch (fmt) {
case "int":flag= isInteger(value); break;
case "float":flag= isFloat(value); break;
case "mobile":flag= isMobile(value); break;
case "email":flag= isEmail(value); break;
case "code":flag= isCode(value); break;
default:flag= true;
}
if(!flag){
errinfo(objid, placeholder + "格式不正确");
}
if (flag){
if ($("#" + objid).parent().children("div:last-child").attr("name") == "error")
{
$("#" + objid).parent().children("div:last-child").remove();
}
}
}
return flag;
}
//校验所有的信息是否为空
function checkAllInput() {
var flag = true;
$("div [name='error']").remove();
$("input[mstype=1]").each(function() {
if ($(this).attr("mstype") == 1) {
checkformat($(this).attr("id"));
}
});
return flag;
}