如何在FireFox中的Javascript中检查无效日期

时间:2016-03-18 13:05:57

标签: javascript angularjs

我想测试无效日期,我写的函数在chrome中工作正常但在Firefox中没有。以下是一些不在FF中工作的例子:

  1. 新日期(' 01/99/2010')=返回有效日期
  2. 新日期(' 99/01/2010')=返回有效日期
  3. var day =' 01',month =' 99',year =' 2010&#39 ;; 新日期(年,月,日)=返回有效日期
  4. var day =' 99',month =' 01',year =' 2010&#39 ;; 新日期(年,月,日)=返回有效日期
  5. 以上方法返回"无效日期"在Chrome中,但不在Firefox中。有谁知道在Firefox中验证日期的正确方法。

    PS:输入字符串可以是 - mm / dd / yyyy或dd / mm / yyyy或yyyy / mm / dd

3 个答案:

答案 0 :(得分:1)

Firefox看起来比Chrome更进一步:

  

注意:Date被称为具有多个构造函数的构造函数   参数,如果值大于它们的逻辑范围(例如13是   作为月值提供或者为分钟值提供70),相邻   价值将被调整。例如。新日期(2013,13,1)相当于   新日期(2014,1,1),都创建了2014-02-01的日期(请注意   月是0基础)。与其他值类似:新日期(2013年,2,1,1,   70)相当于新的日期(2013年,2日,1日,1日,10日),它们都创造了一个   日期为2013-03-01T01:10:00。

来源 - MDN Date documentation

这里强调的是有多个参数。这就是Chrome为Firefox所做的事情的原因:

new Date(2010, 99, 1); - 有效的约会对象。

但是因为:

new Date('01/99/2010');在技术上只是一个参数,它不会因Chrome中的上述规则而失败,但Firefox允许它通过。

考虑到上述情况,以及浏览器之间的不一致,看起来您可能会因为尝试通过Date对象分别编写一个验证器而无法编写日期,月份和年份,如果您需要它在Firefox中工作。

答案 1 :(得分:0)

我会使用内部JavaScript函数来验证日期,因为浏览器会以非常不同的方式处理这些数据类型。

    function isValidDate(date)
    {
    var matches = /^(\d{2})[-\/](\d{2})[-\/](\d{4})$/.exec(date);
    if (matches == null) return false;
    var d = matches[2];
    var m = matches[1] - 1;
    var y = matches[3];
    var checkDate = new Date(y, m, d);
    return checkDate.getDate() == d &&
    checkDate.getMonth() == m &&
    checkDate.getFullYear() == y;
    }

然后你会像这样使用它:

   var d = new Date(2010, 99, 1);
   console.log( isValidDate(d) ); // returns false no valid date

答案 2 :(得分:0)

您可以使用正则表达式。试试这个:

    class OctreeNode {
    public:
        std::vector<std::shared_ptr<OctreeNode>>    Children;
        std::vector<TPoint> Data;
        BoundingBox         Bounds;

        OctreeNode(){}

        OctreeNode(BoundingBox bounds) : Bounds(bounds){
        }

        ~OctreeNode(void){}

        void Split();

    };

    typedef std::shared_ptr<OctreeNode> OctreeNodePtr;


    void OctreeNode::Split()
    {
        Point box[8];
        Bounds.Get8Corners(box);
        Point center = Bounds.Center;

        Children.reserve(8);
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[0], center))));
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[1], center))));
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[3], center))));
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[2], center))));


        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[5], center))));
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[4], center))));
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[6], center))));
        Children.push_back(OctreeNodePtr(new OctreeNode(BoundingBox::From(box[7], center))));
    }



    Octree::Octree(BoundingBox bounds) : Bounds(bounds)
    {
        _root = OctreeNodePtr(new OctreeNode(bounds));
        _root->Split();
    }


    Octree::~Octree()
    {
    }



    bool Octree::InsertPoint(TPoint &p)
    {
        return InsertPoint(p, _root);
    }

    bool Octree::InsertPoint(TPoint &p, const OctreeNodePtr &parent)
    {
        if (parent->Children.size() != 0){
            for (size_t i = 0; i < parent->Children.size(); i++){
                OctreeNodePtr &currentNode = parent->Children[i];
                if (currentNode->Bounds.IsContained(p.ToPoint3d())){
                    return InsertPoint(p, currentNode);
                }           
            }

            // Was not able to insert a point.
            return false;
        }

        BoundingBox newBounds = parent->Bounds;
        newBounds.Extend(p.ToPoint3d());


        // Check for split condition...
        if (parent->Data.size() == MaxPerNode && newBounds.XLength() > 0.01){

            // Split it...thus generating children nodes
            parent->Split();


            // Resize the children arrays so that we don't have to keep allocating when redistributing points..
            for (size_t i = 0; i < parent->Children.size(); i++){
                parent->Children[i]->Data.reserve(parent->Data.size());
            }


            // Distribute the points that were in the parent to its children..
            for (size_t i = 0; i < parent->Data.size(); i++){
                TPoint originalPoint = parent->Data[i];
                if (!InsertPoint(originalPoint, parent)){
                    printf("Failed to insert point\n");
                    break;
                }
            }

            // Insert the current point.
            if (!InsertPoint(p, parent)){
                printf("Failed to insert point\n");
            }


            // Resize the arrays back so it fits the size of the data.....
            for (size_t i = 0; i < parent->Children.size(); i++){
                parent->Children[i]->Data.shrink_to_fit();
            }

            // clear out the parent information
            parent->Data.clear();
            parent->Data.shrink_to_fit();
            return true;
        } else {
            // Node is valid so insert the data..
            if (parent->Data.size() <= 100000){
                parent->Data.push_back(p);
            } else {
                printf("Too much data in tiny node... Stop adding\n");
            }

            return true;
        }


    }


    void Octree::Compress(){
        Compress(_root);
    }

    void Octree::Compress(const OctreeNodePtr &parent){


        if (parent->Children.size() > 0){

            // Look for and remove useless cells who do not contain children or point cloud data.
            size_t j = 0;
            bool removed = false;
            while (j < parent->Children.size()){
                if (parent->Children[j]->Children.size() == 0 && parent->Children[j]->Data.size() == 0){
                    parent->Children.erase(parent->Children.begin() + j);
                    removed = true;
                } else {
                    Compress(parent->Children[j]);
                    ++j;
                }
            }

            if (removed)
                parent->Children.shrink_to_fit();

            return;
        }

        parent->Data.shrink_to_fit();
    }

jsFiddle