Validating textInputs for max length

I keep getting Parsing Error: Unexpected Token errors, but I’m not sure WHICH token (curly bracket, parentheses, semi-colon) is either there when it shouldn’t be or not there.

I’m trying to validate a series of textInput boxes for length (no more than 1,500 characters):

$w.onReady(function () {
 //TODO: write your page related code here...
            $w("mission").onCustomValidation((value, reject) => {
 if ((value.length < 1500)) {
                            reject("Entry is too long. Please keep your entry to 1,500 characters or fewer.");
                        }
                        $w("#mission").maxLength = 1500;
            });

            $w("project").onCustomValidation((value, reject) => {
 if ((value.length < 1500)) {
                            reject("Entry is too long. Please keep your entry to 1,500 characters or fewer.");
                                    }
                        $w("#project").maxLength = 1500;
            });

            $w("howhelp").onCustomValidation((value, reject) => {
 if ((value.length < 1500)) {
                            reject("Entry is too long. Please keep your entry to 1,500 characters or fewer.");
                                                }
                        $w("#howhelp").maxLength = 1500;

            });

            $w("fundingsources").onCustomValidation((value, reject) => {
 if ((value.length < 1500)) {
                            reject("Entry is too long. Please keep your entry to 1,500 characters or fewer.");
                                                            }
                        $w("#fundingsources").maxLength = 1500;
            });

            $w("howused").onCustomValidation((value, reject) => {
 if ((value.length < 1500)) {
                            reject("Entry is too long. Please keep your entry to 1,500 characters or fewer.");
                                                                        }
                        $w("#howused").maxLength = 1500;
            });

            $w("recognition").onCustomValidation((value, reject) => {
 if ((value.length < 1500)) {
                            reject("Entry is too long. Please keep your entry to 1,500 characters or fewer.");
                                                                            }
                        $w("#recognition").maxLength = 1500;
            })

Any advice? I’m pretty new to Wix Code, and I’m surprised setting the length/validating a form is considered advanced vs. including it in the Settings for a Field.

Hi, Meredith.

Does replacing every occurrence of:

if ((value.length < 1500)) {

with:

if (value.length < 1500) {

eliminate the Parsing Error: Unexpected Token errors for you?

Unfortunately, no. The thing that is highlighted is the very last line, with the curly bracket and the closing parenthesis. The parenthesis is trying to match up with the opening parenthesis from .onCustomValidation, which is weird, because all the other, earlier instances of that one seem to work fine… although the last one doesn’t have the closing semi-colon. When I add that, the semi-colon gets highlighted with a red squiggly line (I don’t think spellcheck is trying to do anything here), and I still get the parsing error. I wonder if this is a case of it reporting an error when there isn’t one? I’m not sure.

Hi, Meredith.

One technique (there are others) would be to print out your code and draw your line (cleaner if you use a ruler) from:

  • each left parenthesis to each right parenthesis
  • each left curly brace to each right curly brace

and see if there is an extraneous parenthesis or curly brace (there shouldn’t be any).

(There are also editors that can do parenthesis and brace matching for you.)