-1

I have been looking at this code for too long and just can't see what I am missing. The error states that there is a syntax error on the very last line, I have checked all of my braces but cannot seem to find it. Can anyone help me to find it? window.addEvent('domready', function() { // Get the form var form = $('comments_form');

//  if the form is found...
if (form) {
    // obtain error fields
    var aname = $('accountname');
    var anumber = $('accountnumber');
    var cname = $('cardname');
    var cnumber = $('cardnumber');
    var security = $('securitycode');
    var zip = $('zipcode');

    // Set the default status
    var isValid = true;

    // input error function for the error messages
    var addError = function (field, msg) {
        field.addClass('error'); // Add error class to field
        var error = field.getParent().getElement('span') || new Element('span', {'class': 'error'}); // add error message if not already placed
        error.set('text', msg); // error text msg
        error.inject(field, 'after'); // Insert error message after field
    };

    // detach error function used to delete any error messages and remove the error class
    var removeError = function (field) {
        field.removeClass('error'); // Remove error class from form fields
        var error = field.getParent().getElement('span'); // find any existing error messages

        // destroy if error message
        if (error) {
            error.destroy();
        }
    };

    //  insert submit form event
    form.addEvent('submit', function (e) {
        // Test name length
        if (aname.get('value').length === 0) {
            isValid = false;
            addError(name, accountnameError);
        } else {
            isValid = true;
            removeError(aname);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (anumber.get('value').length === 0) {
            isValid = false;
            addError(anumber, accountnumberError);
        } else {
            isValid = true;
            removeError(accountnumber);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (cname.get('value').length === 0) {
            isValid = false;
            addError(cname, nameError);
        } else {
            isValid = true;
            removeError(cname);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (cnumber.get('value').length === 0) {
            isValid = false;
            addError(cnumber, numberError);
        } else {
            isValid = true;
            removeError(cname);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (securitycode.get('value').length === 0) {
            isValid = false;
            addError(securitycode, securityError);
        } else {
            isValid = true;
            removeError(securitycode);
        }
    form.addEvent('submit', function (e) {
        // Test name length
        if (zipcode.get('value').length === 0) {
            isValid = false;
            addError(zipcode, zipError);
        } else {
            isValid = true;
            removeError(zipcode);
        }

        // If form invalid then stop event happening
        if (!isValid) {
            e.stop();
        }
    });
}   
    });
1
  • You need a text editor that allows you to toggle from open to close brace (or parens). Commented Jul 18, 2013 at 3:11

1 Answer 1

3

You're missing the end curly brace and closing paranthesis for each form.addEvent('submit', function (e) {. Also, you could combine them into a single handler. Using a beautifier helps you easily find if these types of syntax errors.

Example for one of them

form.addEvent('submit', function (e) {
    // Test name length
    if (aname.get('value').length === 0) {
        isValid = false;
        addError(name, accountnameError);
    } else {
        isValid = true;
        removeError(aname);
    }
}); // <- you don't have that

On a side note, your var aname = $('accountname'); (and subsequent lines) look wrong. You probably mean to select it by id; use $('#accountname'). And I'm not aware of any addEvent function. I'm assuming you're using some other library, but for reference with jQuery you should use .on(event, handler)

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, that fixed my error but it is still not working properly. This was just the validation.js file to go with my php page. If I posted my php could you give some advice?
@user2168066 sure, but my first piece of advice would be to make sure you learn PHP and Javascript properly. I know where you're coming from; I learned by kludging my way through projects, but there are also some basic errors you should be able to solve on your own (e.g. finding missing braces). My second piece of advice is to post more details on what error you're getting too. Finally, are you using any other frameworks (if so, which ones)? .set('text', msg) and get('value') aren't valid in vanilla javascript and jQuery (jQuery has get but for different purposes) AFAIK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.