java的常用函数代码 js

本文提供了一系列前端验证函数,包括但不限于邮箱验证、数字验证、日期比较等功能。这些函数可以帮助开发者快速实现表单验证逻辑。

/**
* java版本的escape和 unescape[对应javaScript里的函数]
*/

public class EscapeTool {

public static String escape(String src) {
int i;
char j;
StringBuffer tmp
= new StringBuffer();
tmp.ensureCapacity(src.length()
* 6);
for (i = 0; i < src.length(); i++) {
j
= src.charAt(i);
if (Character.isDigit(j) || Character.isLowerCase(j)
|| Character.isUpperCase(j))
tmp.append(j);
else if (j < 256) {
tmp.append(
"%");
if (j < 16)
tmp.append(
"0");
tmp.append(Integer.toString(j,
16));
}
else {
tmp.append(
"%u");
tmp.append(Integer.toString(j,
16));
}
}
return tmp.toString();
}

public static String unescape(String src) {
StringBuffer tmp
= new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos
= src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch
= (char)Integer.parseInt(
src.substring(pos
+ 2, pos + 6), 16);
tmp.append(ch);
lastPos
= pos + 6;
}
else {
ch
= (char)Integer.parseInt(
src.substring(pos
+ 1, pos + 3), 16);
tmp.append(ch);
lastPos
= pos + 3;
}
}
else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos
= src.length();
}
else {
tmp.append(src.substring(lastPos, pos));
lastPos
= pos;
}
}
}
return tmp.toString();
}
}

// 是否为空,非空返回真,不非为空返回假
function isBlank(str) { var blankFlag = true;
if (str.length == 0) return true;
for (var i = 0; i < str.length; i++) {
if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {
blankFlag
= false;
break;
}
}
return blankFlag;
}
function checkNotNull(theField, fieldName) {
if(isBlank(theField.value)){
alert(fieldName
+ "不可为空!");
if(theField.type!="hidden"){
theField.focus();
}
return false;
}
return true;
}

// 是否为数字
function checkNumber(theField, fieldName) {
var pattern = /^([0-9]|(-[0-9]))[0-9]*((/.[0-9]+)|([0-9]*))$/;
if(theField.value.trim() == "") return true;
if (!pattern.test(theField.value.trim())) {
alert(fieldName
+ "必须为合法数字");
theField.focus();
theField.select();
return false;
}

return true;
}

function isNumber(str) {
var pattern = /^([0-9]|(-[0-9]))[0-9]*((/.[0-9]+)|([0-9]*))$/;
if(str.trim() == "") return false;
if (!pattern.test(str.trim())) return false;
return true;
}

// 是否为指定范围数字
function checkNumberRange(theField, fieldName, min, max) {
if(theField.value.trim() == "") return true;
if (!checkNumber(theField, fieldName)) return false;

if ((min != "") && (theField.value.trim() < min)) {
alert(fieldName
+ "不可小于" + min + "");
theField.focus();
theField.select();
return false;
}

if ((max != "") && (theField.value.trim() > max)) {
alert(fieldName
+ "不可超过" + max + "");
theField.focus();
theField.select();
return false;
}

return true;
}

function isNumberRange(str, min, max) {
if(str == "") return false;
if (!isNumber(str)) return false;

if ((min != "") && (str < min)) {
return false;
}

if ((max != "") && (str > max)) {
return false;
}

return true;
}

// 是否为整数
function checkInteger(theField, fieldName) {
var pattern = /^(/d|(-/d))/d*$/;

if(theField.value.trim() == "") return true;
if (!pattern.test(theField.value.trim())) {
alert(fieldName
+ "必须为整数!");
theField.focus();
theField.select();
return false;
}

return true;
}

function isInteger(str) {
var pattern = /^(/d|(-/d))/d*$/;

if(str.trim() == "") return false;
if (!pattern.test(str.trim())) {
return false;
}
return true;
}

// 是否为指定范围内整数
function checkIntegerRange(theField, fieldName, min, max) {
if(theField.value.trim() == "") return true;
if (!checkInteger(theField, fieldName)) return false;

if ((min != "") && (theField.value.trim() < min)) {
alert(fieldName
+ "不可小于" + min + "");
theField.focus();
theField.select();
return false;
}

if ((max != "") && (theField.value.trim() > max)) {
alert(fieldName
+ "不可超过" + max + "");
theField.focus();
theField.select();
return false;
}

return true;
}

function isIntegerRange(str, min, max) {
if(str == "") return false;
if (!isInteger(str)) return false;

if ((min != "") && (str < min)) {
return false;
}

if ((max != "") && (str > max)) {
return false;
}

return true;
}

// 是否为正数
function checkPositiveNumber(theField, fieldName) {
if(theField.value == "") return true;
if (theField.value.charAt(0) == '-') {
alert(fieldName
+ "必须为正数!");
theField.focus();
return false;
}

return true;
}

// 是否规定的浮点数,包括小数点前的位数和小数点后的位数,beforeDec是小数点前的位数,afterDec是小数点后的为数
function checkDecimalNumber(theField, fieldName, beforeDec, afterDec){
if (theField.value == "") return true;
var pattern ="/^(|[+-]?(0|([1-9]//d{0,"+(beforeDec-1)+"})|((0|([1-9]//d{0,"+(beforeDec-1)+"}))?//.//d{1,"+afterDec+"})){1,1})$/";
if (!eval(pattern).test(theField.value)) {
alert(fieldName
+ "必须为合法数字,允许小数点前"+beforeDec+"位和小数点后"+afterDec+"位之内");
theField.focus();
theField.select();
return false;
}
return true;
}

//使用 prototype 为 String 对象增加一个新方法 length2,找出字符串中有多少个汉字,然后将这个数字加上 length 的值就是我们想要的结果了。
String.prototype.length2 = function() {
var cArr = this.match(/[^/x00-/xff]/ig);
return this.length + (cArr == null ? 0 : cArr.length);
}
// 限制字串最大长度
function checkLength(theField, fieldName, maxLength) {
if(theField.value == "") return true;
if (theField.value.length2() > maxLength) {
alert(fieldName
+ "的字数最多为" + maxLength + "字!");
theField.select();
theField.focus();
return false;
}

return true;
}

// 限制字串长度,注意参数顺序
function checkLength2(theField, fieldName, maxLength, minLength) {
if(theField.value == "") return true;
if (theField.value.length2() > maxLength) {
alert(fieldName
+ "的字数最多为" + maxLength + "字!");
theField.focus();
return false;
}

if ((minLength != "") && (theField.value.length2() < minLength)) {
alert(fieldName
+ "的字数最少为" + minLength + "字!");
theField.focus();
return false;
}

return true;
}

// 所输入字符串是否均为合法字符
//
charBag中为包含所有合法字符的字符串
function checkStrLegal(theField, fieldName, charBag) {
if(theField.value == "") return true;
for (var i = 0; i < theField.value.length; i++) {
var c = theField.value.charAt(i);
if (charBag.indexOf(c) == -1) {
alert(fieldName
+ "含有非法字符(" + c + ")!");
theField.focus();
return false;
}
}

return true;
}

// 所输入字符串是否均为合法字符
//
charBag中为包含非法字符的字符串
function checkStrLegal2(theField, fieldName, charBag) {
if(theField.value == "") return true;
for (var i = 0; i < theField.value.length; i++) {
var c = theField.value.charAt(i);
if (charBag.indexOf(c) > -1) {
alert(fieldName
+ "含有非法字符(" + c +")!");
theField.focus();
return false;
}
}

return true;
}

// 电子邮件验证
function checkEmail(theField) {
var pattern = /^.+@.+/..+$/;

if(theField.value == "") return true;
if (!pattern.test(theField.value)) {
alert(
"请输入合法的电子邮件地址");
theField.focus();
theField.select();
return false;
}

return true;
}

// 是否为只读域(如file,text等域只接受右边按钮选择传回的结果)
function checkReadField() {
alert(
"请点击后面的图标进行选择");
// this.blur();
}
/*
* RoundTo(Digit,How):数值格式化函数,Digit要格式化的数字,How要保留的小数位数。
*/
function RoundTo(Digit,How)
{
Digit
= Math.round(Digit*Math.pow(10,How))/Math.pow(10,How);
return Digit;
}
//去除字符串的前后空格
String.prototype.trim = function()
{
return this.replace(/(^/s+)|/s+$/g,"");
}

//将指定的字段转换为大写
function UpperCase(theField){
theField.value
= theField.value.toUpperCase();
}

//将指定的字段转换为小写
function LowerCase(theField){
theField.value
= theField.value.toLowerCase();
}

//比较两个时间大小,相等或第二个大返回true,第一个大返回false.
//
现在只支持2005-12-30或2005-12-30 10:00:00的时间格式
function DateCompare(theField1,fieldName1,theField2,fieldName2)
{
var d1 = theField1.value;
var d2 = theField2.value;
if (d1==d2) return true; // 相等
var a1 = null;var a2 = null;
var b1 = null;var b2 = null;
if (d1.length==10){
d1
+= " 00:00:00";
}
a1
=d1.substring(0,10).split("-");
a2
=d1.substring(11).split(":");
if (d2.length==10){
d2
+= " 00:00:00";
}
b1
=d2.substring(0,10).split("-");
b2
=d2.substring(11).split(":");

for (i=0;i<3;i++){
if( a1[i].charAt(0) == '0' ) { a1[i] = a1[i].substring(1,2); }
if( b1[i].charAt(0) == '0' ) { b1[i] = b1[i].substring(1,2); }
if (parseInt(a1[i])>parseInt(b1[i])){
alert(fieldName1
+"不能大于"+fieldName2);
return false;
}
if (parseInt(a1[i])<parseInt(b1[i])){
return true;
}
}
for (i=0;i<3;i++){
if( a2[i].charAt(0) == '0' ) { a2[i] = a2[i].substring(1,2); }
if( b2[i].charAt(0) == '0' ) { b2[i] = b2[i].substring(1,2); }
if (parseInt(a2[i])>parseInt(b2[i])){
alert(fieldName1
+"不能大于"+fieldName2);
return false;
}
if (parseInt(a2[i])<parseInt(b2[i])){
return true;
}
}
}

//将给定的字符串中的&字符替换成@@
function ConvertStr(s){
var i;
var s2 = s;

while(s2.indexOf("&")>0){
i
= s2.indexOf("&");
s2
= s2.substring(0, i) + "@@" + s2.substring(i + 1, s2.length);
}
return s2;
}

//将给定的字符串中的@@字符替换成&
function ReConvertStr(s){
var i;
var s2 = s;

while(s2.indexOf("@@")>0){
i
= s2.indexOf("@@");
s2
= s2.substring(0, i) + "&" + s2.substring(i + 2, s2.length);
}
return s2;
}
//将给定的字符串中的单双引号转义
function ForamtValue(oStr)
{
switch(typeof(oStr))
{
case "date" :
//直接toString()转换,可以加入丰富的显示方式
sStr = (new Date(oStr)).toString();
break;
default :
sStr
= String(oStr);
}
sStr
= sStr.replace(//"/g,"&#34;"); //输入框中显示双引号问题
sStr = sStr.replace(//'/g,"&#39;"); //输入框中显示单引号问题
return sStr;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值