js - onblur java - numberUtil

98 篇文章 0 订阅
				var table2 = document.getElementById("gridTable2");
				var row = null;
				var cell = null;
				var value = "";
				var text = null;
				var textArea = null;
				var limitsize = null;
				var lable = null;
				var span = null;
				for (var i = 1; i < table2.rows.length; i++) {
					row = table2.rows[i];

					for (var j = 5; j < row.cells.length; j++) {
						cell = row.cells[j];
						value = cell.innerText + "";

						if (j == 5) {
							limitsize = value;
						} else if (j == 9) {
							text = document.createElement("input");
							text.setAttribute("id", "text" + i);
							text.setAttribute("type", "text");
							text.setAttribute("placeholder", "评分");
							text.setAttribute("limitsize", limitsize);
							text.setAttribute("style", "width: 50px;margin-top: 15px;margin-bottom: 15px;");
							text.onblur = function()
							{
								var msgBox = document.getElementById("span" + this.id.substring(4));
								
								// 校验只能输入数值
								var num = new Number(this.value);
								if (isNaN(num))
								{
									msgBox.innerHTML = "请输入合法数值。";
									this.focus();
									return;
								}
								else
								{
									msgBox.innerHTML = "";
								}
								
								// 校验只能输入大于0的数
								if (num < 0)
								{
									msgBox.innerHTML = "请输入大于0的数值。";
									this.focus();
									return;
								}
								else
								{
									msgBox.innerHTML = "";
								}
								
								// 小数位数两位
								var strValue = this.value.toString();
								console.log(strValue);
								
								var index = strValue.indexOf(".");
// 								console.log("index = " + index);
								if (index >= 0)
								{
									strValue = strValue.substring(index + 1);
// 									console.log("strValue = " + strValue);
									
									if (strValue.length > 2)
									{
										msgBox.innerHTML = "请保留两位小数。";
										this.focus();
										return;
									}
									else
									{
										msgBox.innerHTML = "";
									}
								}
								else
								{
									msgBox.innerHTML = "";
								}
								
								// 校验评分不能大于分值
								var max = this.getAttribute("limitsize");
// 								console.log(max);
								if (this.value > max)
								{
									msgBox.innerHTML = "[评分]数值不能大于[分值]数值。";
									this.focus();
									return;
								}
								else
								{
									msgBox.innerHTML = "";
								}
							}
							
							
							
							if(value != " "){
								text.value = value;
							}
							
							//
							span = document.createElement("span");
							span.setAttribute("id", "span" + i);
							span.setAttribute("style", "color:red");
							//style='color:red'
							
							//
							cell.innerHTML = "";
							
							//
							cell.appendChild(text);
							cell.appendChild(span);
						} else if (j == 10) {
							textArea = document.createElement("textarea");
							textArea.setAttribute("style", "width: 270px;height: 50px;");
							textArea.setAttribute("placeholder", "请输入评审意见");
							if(value != " "){
								textArea.value = value;
							}
							cell.innerHTML = "";
							cell.appendChild(textArea);
						}
					}
				}
				
				/*
				<tr role='row' class='ui-widget-content jqgrow ui-row-ltr ui-priority-secondary'>
					<td colspan='3' role='gridcell' style='text-align:left;vertical-align: middle;' title='是否推荐”义务教育管理标准化学校”最值得宣传的学校'>是否推荐”义务教育管理标准化学校”最值得宣传的学校</td>
					<td colspan='5' role='gridcell' style='text-align:left;vertical-align: middle;'>
						<input style='vertical-align: middle;margin-left: 50px;' width='30px' type='radio' name='isRecommend' id='isRecommend_ok' value='1'>
							<label for='isRecommend_ok'>推荐</label>
						</input>
						<input style='vertical-align: middle;margin-left: 100px;' width='30px' type='radio' name='isRecommend' id='isRecommend_no' value='0'>
							<label for='isRecommend_no'>不推荐</label>
						</input>
					</td>
				</tr>
				*/

				$("#gridTable2").append("<tr role='row' class='ui-widget-content jqgrow ui-row-ltr ui-priority-secondary'><td colspan='3' role='gridcell' style='text-align:left;vertical-align: middle;' title='是否推荐”义务教育管理标准化学校”最值得宣传的学校'>是否推荐”义务教育管理标准化学校”最值得宣传的学校</td><td colspan='7' role='gridcell' style='text-align:left;vertical-align: middle;'><input style='vertical-align: middle;margin-left: 50px;' width='30px' type='radio' name='isRecommend' id='isRecommend_ok' value='1'><label for='isRecommend_ok'>推荐</label></input><input style='vertical-align: middle;margin-left: 100px;' width='30px' type='radio' name='isRecommend' id='isRecommend_no' value='0'><label for='isRecommend_no'>不推荐</label></input> </td></tr>");






package util;

public class NumberUtil
{
	/**
	 * 判断字符串是否是数值
	 * 
	 * 
	System.out.println(isDouble("232BSDSD.333"));
	
	
	Exception in thread "main" java.lang.NumberFormatException: For input string: "232BSDSD.333"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
	at java.lang.Double.parseDouble(Double.java:540)
	at com.sirdc.modules.sys.util.NumberValidationUtils.isDouble(NumberValidationUtils.java:65)
	at com.sirdc.modules.sys.util.NumberValidationUtils.main(NumberValidationUtils.java:71)
	 * 
	 * 
	 * @author ZengWenFeng
	 * @date 2017-10-18
	 */
	public static boolean isDouble(String str)
	{
		try
		{
			Double.parseDouble(str);
			return true;
		}
		catch (NumberFormatException e)
		{
			return false;
		}
	}

	/**
	 * -34343.89
	 * 343.1
	 * 
	 * @author ZengWenFeng
	 * @date 2017-10-18
	 * @param str
	 * @return
	 */
	public static boolean isDoubleWithTwoDecimalPlaces(String str)
	{
		if (isDouble(str))
		{
			str = str.trim();
			int index = str.indexOf(".");

			if (index >= 0)
			{
				str = str.substring(index + 1);

				if (str.length() > 2 || str.length() < 0)
				{
					return false;
				}
				else
				{
					return true;
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	/**
	 * test
	 * 
	 * @author ZengWenFeng
	 * @date 2017-10-18
	 * @param args
	 */
	public static void main(String[] args)
	{
		System.out.println(isDouble(""));//false
		System.out.println(isDouble("03"));//true  3.0
		System.out.println(isDouble("03.8733"));//true  3.8733

		System.out.println("--------------------------");
		System.out.println(isDoubleWithTwoDecimalPlaces("03.858"));
		System.out.println(isDoubleWithTwoDecimalPlaces("03.8"));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值