您的位置:首页 > 其它

自定义验证(可以验证输入的是否为数字、浮点数、文本框中字符的长度、日历控件)

2013-03-18 14:57 411 查看
直接将该js复制到就是文件,在引入到web页面中,

使用指导:<asp:TextBox runat="server" ID="txtTest1" input_type="int" />,只要在文本控件中加属性:input_type="?"即可,?的取值有:int,decimal 分别用于验证整数和小数

//一.完成后执行 $(document).ready(function () { //1.正整数输入限制 $(':text[input_type=int]').each(function () { var $this = $(this); $this.keydown(function (event) { //(数字)||(数字键盘)||回退键 if (!((event.which >= 48 && event.which <= 57) || (event.which >= 96 && event.which <= 105) || event.which == 8)) { event.preventDefault(); } }); });

//2.非负浮点数输入限制 $(':text[input_type=decimal]').each(function () { var $this = $(this); var valueOfThis = ''; $this.focus(function (event) { valueOfThis = $this.val(); }); $this.keydown(function (event) { //(数字)||(数字键盘)||小数点||回退键 if (!((event.which >= 48 && event.which <= 57) || (event.which >= 96 && event.which <= 105) || event.which == 190 || event.which == 8)) { event.preventDefault(); } }); $this.blur(function () { var reg = /^\d+(\.\d+)?$/; //非负浮点数正式表达式 var $this = $(this); if ($this.val() != '' && reg.exec($this.val()) == null) { $this.val(valueOfThis); alert('输入的数字格式不正确!请检查!'); } }); });

//3.验证TextMode="MultiLine"的textbox的长度 //3.1给string增加个len ()方法,计算string的字节数 String.prototype.len = function () { return this.replace(/[^\x00-\xff]/g, "xx").length; } //3.2判断和截断 $('textarea').each(function () { var $this = $(this); $this.blur(function () { if ($this.attr('max_length') != '' && $this.val().len() > $this.attr('max_length')) { $this.val($this.val().substr(0, $this.val().replace(/[^\x00-\xff]/g, '#&').substr(0, $this.attr('max_length') - 1).replace(/#&/g, 'y').length)); alert('请检查字符长度,应不超过' + $this.attr('max_length') + '个字符!超出字符已被截断!'); } }); });

//4.class=lableDesc的td的样式 $('td.lableDesc').css({ whiteSpace: 'nowrap' });

//5.日期控件 $(':text[input_type=calendar]').each(function () { var $this = $(this); $.getScript('../Script/JQuery/ui/jquery.ui.datepicker.js', function () { $this.datepicker({ dateFormat: 'yy-mm-dd', closeText: '关闭', prevText: '<上月', nextText: '下月>', currentText: '今天', monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], monthNamesShort: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'], dayNames: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'], dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'], dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'], weekHeader: '周', firstDay: 1, isRTL: false, showMonthAfterYear: true, yearSuffix: '年' }); }); });

//6.tabs $('#jquery_ui_tabs').tabs(); });

//二.是否可以发送 function IsCanSend(msg, selecterExpression) {

var num = 0; var str = ""; $(selecterExpression).each(function () { var $this = $(this); if ($this.val() == "") { num++; str += "*" + $this.attr('msg') + "不能为空!\r\n"; } }); if (num > 0) { alert(str); return false; } else { if (window.confirm(msg)) { return true; } else { return false; } } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐