巧用weui.topTips验证数据的实例
作者:jingxian 发布时间:2023-08-12 03:00:51
标签:weui,toptips
场景一、有一个输入金额的场景,这个金额需要验证,验证说明如下:
不能为空格;
不能为0;
不能为汉字;
不能为其它字符;
不能大于200;
唯一可以的是,只有输入3~199之间的数字,下面的确定按钮才会显示,否则,隐藏这个按钮。
HTML:
<!--医生问诊金额-->
<div class="weui-jiaj-panel">
<div class="weui-jiaj-money-box dialog js_show">
<div class="weui-jiaj-money-box-btn">
</div>
<div class="weui-jiaj-money-box-three">
<div class="weui-flex__item">
<a id="showMoney" href="javascript:;" rel="external nofollow" class="weui-btn weui-btn_mini weui-btn_default">其它</a>
</div>
</div>
</div>
</div>
<!--其它金额-->
<div class="weui_dialog_alert" id="showMoneyDialog" style="display: none;">
<div class="weui_mask"></div>
<div class="weui_dialog">
<div class="weui_dialog_hd"><strong class="weui_dialog_title">其它金额</strong></div>
<div class="weui_dialog_bd">
<div class="weui-jiaj-dialog-panel">
<div class="weui-cell">
<div class="weui-cell__bd">
<input id="dialogPrice" type="text" required class="weui-input" placeholder="¥10" />
</div>
</div>
</div>
</div>
<div class="weui_dialog_ft">
<div id="otherPriceBtn" class="weui_btn_dialog primary">确定</div>
</div>
</div>
</div>
JS:
<script>
//设置其它金额
var doctorPrices = [{
"doctorPrice": "5"
}, {
"doctorPrice": "10"
}, {
"doctorPrice": "15"
}, {
"doctorPrice": "20"
}, {
"doctorPrice": "30"
}, {
"doctorPrice": "60"
}];
var userId = $.cookie('doctorId');
$(function() {
selectedPrice();
});
var page = $('.page'); //顶层div
var panel = page.find('weui-jiaj-panel');
function selectedPrice() {
var $titleHtml = '';
for(var a = 0; a < doctorPrices.length; a++) {
var priceName = doctorPrices[a].doctorPrice;
//点周weui_btn_dialog隐藏
$titleHtml += '<button class="price_btn weui-btn weui-btn_mini weui-btn_warn"' + 'name=' + priceName + '>' + priceName + '</button>';
$('.price_btn').css('margin', '5px');
}
$('.weui-jiaj-money-box-btn').append($titleHtml);
//选择金额
$('.price_btn').click(function() {
var titleValue = $(this).attr('name'); //$(this)表示获取当前被点击元素的name值
var data = {
userId: userId,
price: titleValue
};
data = JSON.stringify(data);
$.ajax({
data: {},
dataType: 'json',
type: "post",
url: postDoctorPrice().replace("{userId}", userId).replace("{price}", titleValue),
contentType: 'application/json; charset=utf-8',
success: function(data) {
if(data && data.status == '200') {
weui.topTips('提交成功');
}
},
error: function(data) {
location.href = 'doctor_wode.html';
}
});
});
//其它金额
$('#otherPriceBtn').on('click', function(e) {
var otherPrice = $('#dialogPrice').val();
otherPrice = parseInt(otherPrice);
otherPrice = otherPrice.toString();
console.log("其它金额" + otherPrice);
var data = {
userId: userId,
price: otherPrice
};
data = JSON.stringify(data);
$.ajax({
data: {},
dataType: 'json',
type: "post",
url: postDoctorPrice().replace("{userId}", userId).replace("{price}", otherPrice), //post 时url带参数
contentType: 'application/json; charset=utf-8',
success: function(data) {
if(data && data.status == '200') {
weui.topTips('设置成功!');
}
},
error: function(data) {
location.href = 'doctor_wode.html';
}
});
});
}
//验证
$('input').on('blur',function(){
var value = this.value;
var regChinese = new RegExp("[\\u4E00-\\u9FFF]+","g");
//字符串不能为空
if(value.length == 0) {
$('#otherPriceBtn').hide();
weui.topTips('不能为空');
//字符串是否为“空”字符即用户输入了空格
}else if(value.replace(/(^s*)|(s*$)/g, "").length ==0){
$('#otherPriceBtn').hide();
weui.topTips('不能为空');
//字符串是否为空或者全部都是空格
}else if(value == null){
$('#otherPriceBtn').hide();
weui.topTips('不能为null');
//字符串是否为汉字
}else if(regChinese.test(value)){
$('#otherPriceBtn').hide();
weui.topTips('不能输入汉字');
//字符串不能为0
}else if(parseInt(value) == 0){
$('#otherPriceBtn').hide();
weui.topTips('不能为0');
//不能大于200
}else if(parseInt(value) > 200){
$('#otherPriceBtn').hide();
weui.topTips('自定义金额不能大于200元');
//自定义金额只能是数字
}else if(typeof(parseInt(value))){
$('#otherPriceBtn').show();
}
})
</script>
场景二、所有违反规距的都有信息提示,但是“确定”按钮不隐藏,只是删除它的click事件,只有符合条件的才可以跳转
//验证
$('input').on('blur', function() {
var value = this.value;
var regChinese = new RegExp("[\\u4E00-\\u9FFF]+", "g"); //汉语
var specialSymbol =/[`~!@#$%^&*_+<>{}\/'[\]]/im; //特殊符号
//字符串不能为空
if(value.length == 0) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('不能为空,请重新输入');
}, 500);
//字符串是否为“空”字符即用户输入了空格
} else if(value.replace(/(^s*)|(s*$)/g, "").length == 0) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('不能为空,请重新输入');
}, 500);
//字符串是否为空或者全部都是空格
} else if(value == null) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('不能为空,请重新输入');
}, 500);
//字符串是否为汉字
} else if(regChinese.test(value)) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('不能输入汉字,请重新输入');
}, 500);
//字符串不能为0
} else if(parseInt(value) == 0) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('不能为0,请重新输入');
}, 500);
//小于3
} else if(parseInt(value) < 4) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('自定义金额不能小于3,请重新输入');
}, 500);
//不能大于200
} else if(parseInt(value) > 200) {
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('自定义金额不能大于200,请重新输入');
}, 500);
} else if(specialSymbol.test(value)){
//禁止输入特殊字符
$('#otherPriceBtn').unbind('click');
setTimeout(function() {
$('.hide-description').css('display', 'block').text('不可输入!@#¥%……&*特殊字符!');
}, 500);
//自定义金额只能是数字
} else if(typeof(parseInt(value))) {
setTimeout(function() {
$('.hide-description').css('display', 'block').text('你设置的金额为' + value);
}, 500);
//其它金额
$('#otherPriceBtn').on('click', function(e) {
var otherPrice = $('#dialogPrice').val();
otherPrice = parseInt(otherPrice);
otherPrice = otherPrice.toString();
console.log("其它金额" + otherPrice);
var data = {
userId: userId,
price: otherPrice
};
data = JSON.stringify(data);
$.ajax({
data: {},
dataType: 'json',
type: "post",
url: postDoctorPrice().replace("{userId}", userId).replace("{price}", otherPrice), //post 时url带参数
contentType: 'application/json; charset=utf-8',
success: function(data) {
if(data && data.status == '200') {
weui.topTips('设置成功!');
}
},
error: function(data) {
location.href = 'doctor_wode.html';
}
});
});
}
})
0
投稿
猜你喜欢
- Access爱好者以会VBa为荣。我觉得这不是好现象。vba只是vb的子集,有着很多限制,比如不支持继承,不支持指针,不支持子界类型等。使用
- 本文实例讲述了Python多线程原理与用法。分享给大家供大家参考,具体如下:先来看个栗子:下面来看一下I/O秘籍型的线程,举个栗子——爬虫,
- <%@ Language=VBScript %><%Option Explicit %><%Dim strUR
- < SCRIPT LANGUAGE="VBScript"> < 
- HTML 5 和 XHTML 2规范草稿公布以来,一直存在很大的争议。HTML 5是由包括Google、Mirosoft、Mozilla、O
- 如何用Response.Write调用代替内嵌表达式?我们可以利用下面的代码,注意:代码的每一行对响应流有一次写操作,所有的代码都包含在一个
- 做网页时(其实是网页木马呵呵),最让人烦恼的是自己辛辛苦苦写出来的客户端IE运行的javascript代码常常被别人轻易的拷贝,实在让自己的
- 在SQL Server数据库中,如果执行Transact-SQL时出现了错误,我们可以使用两种捕捉错误的方法解决此问题,一种是在客户端代码(
- 这个仿msn的右下角popup提示窗口效果很久以前收集的,现在整理出来给大家分享,需要的朋友可以拿去用,特点,提示窗口内容和js代码分离容易
- 本文实例讲述了python列表操作之extend和append的区别。分享给大家供大家参考。具体如下:li = ['a',
- 引言今年互联网的就业环境真的好糟糕啊,好多朋友被优化。我们平常在工作中除了撸好代码,跑通项目之外,还要注意内外兼修。内功和招式都得练👌,才能
- 一、事件捕捉(Event Capture)的实现问题首先在说这件事前,先感谢一下Realazy。 W3C DOM Level2的事
- 摸到她了!青翠的衣衫,奶白的肌肤,捧在手上的感觉真是太好了,心里美滋滋的。《悟透JavaScript》,一本偶然之作,终于成书并出版了。本书
- 在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致
- 部署网站前查看一下系统是否已经安装CGI1、启动iis服务器,打开IIS服务器打开IIS服务器,点击网站,右击“添加网站”2、创建网站点击“
- 我的Windows 8.1 环境1.下载安装Python 2.7.6在Python官方网站中下载Python2.7.6的Windows安装包
- 第一章:基本的圆角框第二章:透明圆角化背景图片第三章:圆角化图片 第四章:CSS圆角框组件 V1.0在上面的案例中,我只给出最为原始的圆角框
- 在MySQL经历了2008年Sun的收购和2009年Oracle收购Sun的过程中,基本处于停滞发展的情况,在可以预见的未来,MySQL是肯
- 如果你的网站涉及个人隐私或者机密性非公开的网页,怎样告诉禁止搜索引擎收录抓取,下面侯庆龙说下以下方法,希望对不想被搜索引擎收录抓取网站有所帮
- 代码如下:USE TestDB declare @conversation uniqueidentifier while exists (s