日期:2014-05-16  浏览次数:20399 次

javascript验证

为了提交web页面开发效率,自己封装了一套web页面输入框验证方法:

1、首先是ArrayList对象

该ArrayList对象和java中ArrayList对象用法基本一样,包含了java中ArrayList中大部分方法,为存取对象提供了便利。

/**********************************************************
* JavaScript实现的ArrayList类 
* 
* @author {yangl}
* @version $Revision: 0.5 $ $Date: 2008/04/02 15:00:00 $
* @description
* Method:
* add(element);
* addElementAt(index, element);
* contains(element);
* get(index);
* isEmpty(index);
* indexOf(element);
* lastIndexOf(element);
* remove()
* setElementAt(index, element);
* size();
* toString();
* @example
* var arrList = new ArrayList();
* //var arrList = new ArrayList(10);
* arrList.add("000");
* arrList.add("001");
* arrList.add("002");
*
*********************************************************/
// JavaScript ArrayList
/**
Method:
        add(element);
        addElementAt(index, element);
        contains(element);
        get(index);
        isEmpty(index);
        indexOf(element);
        lastIndexOf(element);
        remove(index);
        setElementAt(index, element);
        size();
        toString();
*/
/**
Example:
        var arrList = new ArrayList();
        //var arrList = new ArrayList(10);
        arrList.add("000");
        arrList.add("001");
        arrList.add("002");
*/
var ArrayList = function () {
    var args = ArrayList.arguments;
    var initialCapacity = 10;
    
    if (args != null && args.length > 0) {
        initialCapacity = args[0];
    }
    
    var elementData = new Array(initialCapacity);
    var elementCount = 0;
    
    this.size = function () {
        return elementCount;
    };
    
    this.add = function (element) {
        //alert("add");
        ensureCapacity(elementCount + 1);
        elementData[elementCount++] = element;
        return true;
    };
    
    this.addElementAt = function (index, element) {
        //alert("addElementAt");
        if (index > elementCount || index < 0) {
            alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);
            return;
            //throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
        }
        ensureCapacity(elementCount + 1);
        for (var i = elementCount + 1; i > index; i--) {
            elementData[i] = elementData[i - 1];
        }
        elementData[index] = element;
        elementCount++;
    };
    
    this.setElementAt = function (index, element) {
        //alert("setElementAt");
        if (index > elementCount || index < 0) {
            alert("IndexOutOfBoundsException, Index: " + index + ", Size: " + elementCount);
            return;
            //throw (new Error(-1,"IndexOutOfBoundsException, Index: "+index+", Size: " + elementCount));
        }
        elementData[index] = element;
    };
    
    this.toString = function () {
        //alert("toString()");
        var str = "{";
        for (var i = 0; i < elementCount; i++) {
            if (i > 0) {
                str += ",";
            }
            str += elementData[i];
        }
        str += "}";
        return str;
    };
    
    this.get = function (index) {
        //alert("elementAt");
        if (index >= elementCount) {
            alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);
            return;
            //throw ( new Error( -1,"ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount ) );
        }
        return elementData[index];
    };
    
    this.remove = function (index) {
        if (index >= elementCount) {
            alert("ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount);
            //return;
            throw (new Error(-1, "ArrayIndexOutOfBoundsException, " + index + " >= " + elementCount));
        }
        var oldData = elementData[index];
        for (var i = index; i < elementCount - 1; i++) {
            elementData[i] = element