巧用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';
}
});
});
}
})


猜你喜欢
- 楔子在 Python3.6 之前,格式化字符串一般会使用百分号占位符或者 format 函数,举个例子:name = &
- 描述remove() 函数用于移除列表中某个值的第一个匹配项。语法remove()方法语法:list.remove(obj)参数obj --
- 如下所示:import numpy as np# 等差数列print(np.linspace(0.1, 1, 10, endpoint=Tr
- import timenow_time = time.time()print(now_time)结果是1594
- openpyxlopenpyxl模块属于第三方模块,是一个在 python 中能够处理 excel 文件的模块,还有比较出名的是xlrd、x
- python 获取蓝牙设备类型扫描蓝牙设备获取到的信息中,无法判断扫描到的蓝牙设备属于什么类型的设备。扫描蓝牙信息使用的是python 里面
- 本文实例讲述了python中__slots__的用法。分享给大家供大家参考。具体分析如下:定义__slots__ 后,可以再实例上分配的属性
- 继续上一篇,针对Bootstrap Metronic菜单栏整理的笔记分享给大家,供大家参考,具体内容如下1.简介1) .环境配置
- 猜测下面这段程序的输出:class A(object): def __init__(self):
- 我们打包APP需要用到HBuilder,所以先讲解如何安装使用HBuilder的下载与安装HBuilder的官网下载地址:https://w
- https://docs.python.org/3/library/function.html #python官方网址# 取绝对值print
- Matlab函数对应关系(Numpy)首先给出官网链接,其中详细说明了在Python下如何用Numpy实现Matlab下相同的函数功能。博主
- 问题描述当前使用的PyCharm社区版版本号2022.1.2,配置镜像源时,没有manage repositories解决方案:镜像源:清华
- 今天我们用python和pygame实现一个乒乓球的小游戏,或者叫弹珠球游戏。笑脸乒乓球游戏功能介绍乒乓球游戏功能如下:乒乓球从屏幕上方落下
- 我想大多数的人在编写ASP程序的时候,都碰到过类似的错误信息: Error Num
- 源代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/
- 目录1.任务要求2.简单设计3.模块实现4.总结由于一些小原因,被迫开始了tkinter一次实战演练。在此做一些记录,总结以及给自己留一些轮
- #这是Python中的一个字典 dic = { 'str': 'this is a string',
- 使用ASP设置指定站点CPU最大使用程度'=============================================
- 近期对数据库进行巡检,发现数据库业务用户(非 SYS/Public)下存在失效对象。对失效对象进行分析,主要包括失效的视图、物化视图、函数、