在UltraWebGrid中验证填入数据

本文介绍了一个使用客户端事件验证用户输入的案例。通过BeforeExitEditModeHandler事件,在用户离开单元格前验证输入的有效性,确保数据质量。示例展示了如何阻止非法日期和超出范围的时间输入。
摘要由CSDN通过智能技术生成

官方例子:


Summary It may prove beneficial to validate user input within the client-side events. The BeforeExitEditModeHandler allows for a perfect time to validate the the input and "lock" the user on the cell until proper data has been entered. Additional Information First, add a handler for the client-side event, BeforeExitEditModeHandler. This property can be set either in the Property Pages under displaylayout>ClientsideEvents>BeforeExitEditModeHandler or in code: UltraWebGrid1.DisplayLayout.ClientSideEvents.BeforeExitEditModeHandler = "BeforeExitEditMode" This event receives two parameters: gridname and cell id. To keep the user within the cell, return true to cancel the event. Here is a sample that will not allow a user to enter a date that is after today for a birthdate: //create a global variable so alertbox does not appear twice. box appears twice since in the BeforeEndEditMode you are calling alert(message) which tries to end the edit as well //without this then you would find yourself in an infinite loop or have an alert appear twice. var oldvalue=null; function beforeExitEditMode(gridName,cellID) { //get the cell cell=igtbl_getCellById(cellID); //get the grid grid=igtbl_getGridById(gridName); //check to see if the user is in the birthdate column if(cell.Column.Key=="BirthDate") { //get today's date var today=new Date(); //get the value of the current cell var valDate=new Date(cell.getValue()); if(valDate < today) { //if the date is before today then allow them to exit edit mode return 0; } //won't get here unless date is greater then today since a valid date would have exited the function above //the next line is neccesarry so we know that this is the first time the user is coming through the function and //we avoid duplicate alert messages if(oldvalue==null) { //set oldvalue to current value so this is not hit next time through and display alert box oldvalue=valDate; alert("BirthDate can't be in the future"); } //if the user has gotten this far then value is invlaid so return 1 to cancel the exiting of edit mode return 1; } }


我自己写的,感觉写的不太优雅,比较丑陋,但是照着官方的写总是满足不了需求

var oldVal function Grid_BeforeExitEditModeHandler(gridName, cellID) { //get the cell cell = igtbl_getCellById(cellID); //get the grid grid = igtbl_getGridById(gridName); if (cell.Column.Key == "operatingtimeperday") { oldVal = parseFloat(cell.getValue()); } } function Grid_AfterExitEditModeHandler(gridName, cellID) { //get the cell cell = igtbl_getCellById(cellID); //get the grid grid = igtbl_getGridById(gridName); //check to see if the user is in the birthdate column if (cell.Column.Key == "operatingtimeperday") { //get the value of the current cell var val = parseFloat(cell.getValue()); if (val > 24) { alert("输入时间不可大于24小时"); cell.setValue(oldVal); return 1; } if (val < 24) { return 0; } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值