I'm using LiveValidation (http://livevalidation.com/) to validate a form on my site for a page url. My conditions are:
So far I have this:
var formName = new LiveValidation("sitePages-name");
formName.add(Validate.Presence);
formName.add(Validate.Format,{ pattern: /^[a-zA-Z]/, failureMessage: "Must start with a letter." } );
formName.add(Validate.Exclusion, {within: [' '], partialMatch: true, failureMessage: "Spaces are not allowed."});
formName.add(Validate.Format,{ pattern:/[a-zA-Z0-9-_]+$/, failureMessage: "Only alpha-numeric characters, dashes and underscores."})
The first and second conditions are met. I am struggling with the third and fourth conditions of "No spaces" and "Only alpha-numeric characters, dashes and underscores."
I tried to do the space and fourth condition by regex but it wasn't working so I just separated the spaces into the Exclusion for now. The pattern for the fourth condition works if the character you JUST typed is not one in the regex pattern. But if you keep typing "valid" characters and have an "invalid" character previously, the error goes away and it becomes valid.
Does anyone know how to do the pattern properly to catch it if there are ANY "non-valid" characters in the input box? It would also be great if I can merge the no space condition with the pattern if possible.
/^[a-zA-Z][a-zA-Z0-9_\-]*$/
.
[a-zA-Z]
means letter.[a-zA-Z0-9_\-]
means letter, digit, dash or underscore.*
means repeat 0 or more times^
is the start of the string$
is the end of the stringThis reads out as: At the start of the string, match a latter, then match zero or more letters, digits or underscore characters, then match the end of the string.