Skip to main content
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Tweeted twitter.com/#!/StackCodeReview/status/443426781341954048
Source Link
user34330
user34330

A class for form data validation

I don't want any feedback on the regexes as I know what needs to be updated here. Also, don't need any feedback on naming conventions.

I'm looking for feedback on the structure and correctness of the class.

/***************************************************************************************************
**SForm - validates and manipulates form data
*/
    var SForm = $A.Class.create({
        Name: 'SForm',
         S: {
            domain:    /:\/\/(www\.)?([\.a-zA-Z0-9\-]+)/,
            url:       /:\/\/(www\.)?[\x00-\x7F]{1,1800}\.[\x00-\x7F]{1,200}/,
            email:     /\S{1,64}@[\x00-\x7F]{1,255}\.[\x00-\x7F]{1,255}/,
            tweet:     /\S{1,40}/,
            title:     /\S{1,32}/,
            name:      /\S{1,64}/,
            pass:      /\S{6,20}/,
            pre_url:   /(http:)|(https:)\/\//,
            full:      /\S+/,
            google:    'https://plus.google.com/_/favicon?domain='
        },
        constructor : function (form_elements) {
            this.form = {};
            $A.someKey(form_elements, function (val) {
                if (val.type !== 'checkbox') {
                    this.form[val.name] = val.value;
                } else if (val.type === 'checkbox') {
                    this.form[val.name] = val.checked;
                }
            }, this);
        },
        get: function (key) {
            return this.form[key];
        },
        set: function (key, value) {
            this.form[key] = value;
        },
        getObj: function () {
            return this.form;
        },
        checkField: function (key) {
            return this.S[key].test(this.form[key]);
        },
        checkFull: function () {
            var key;
            for (key in this.form) {

                // if it is not a boolean and it is not full
                if (!$A.isBoolean(this.form[key]) && !this.S.full.test(this.form[key]) ) {
                    return false;
                }
            }
            return true;
        },
        checkFullAM: function () {
            var key;
            for (key in this.form) {

                // if the fields is empty and it is not the tag field
                if (key !== "tag" && !this.S.full.test(this.form[key])) {
                    return false;
                }
                
                // if no tag is set, set one by default
                if (key === "tag" && !this.S.full.test(this.form[key])) {
                    this.form[key] = "no-tag";
                }
            }
            return true;
        },
        addURLPrefix: function () {

            // if there is no prefix, add http by default
            if (!this.S.pre_url.test(this.form.url)) {
                this.form.url = 'http://' + this.form.url;
            }
        },
        setDomain: function () {
            var domain = this.form.url.match(this.S.domain);
            if (domain) {
                this.form.domain = domain[2];
            }
        },
        setFaviconTemp: function () {
            this.form.favicon = this.S.google + this.form.domain;
        }
    }, 'constructor');