Livevalidation - Regex issue

 ̄綄美尐妖づ 提交于 2019-12-11 08:56:48

问题


I'm using LiveValidation (http://livevalidation.com/) to validate a form on my site for a page url. My conditions are:

  • Cannot be blank
  • Must start with a letter
  • No spaces allowed
  • Only alpha-numeric characters, dashes, and underscores

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.


回答1:


/^[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 string

This 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.




回答2:


Not having used that plug-in, I take it that subsequent validations added via add are handled in order.

In which case:

var formName = new LiveValidation("sitePages-name");
// Must be present
formName.add(Validate.Presence);
// Must start with A-Z or a-z
formName.add(Validate.Format,{ pattern: /^[a-zA-Z]/, failureMessage: "Must start with a letter." } );
// Must only have A-Z, a-z, 0-9, _, or -
formName.add(Validate.Format,{ pattern:/.[a-zA-Z0-9_\-]+$/, failureMessage: "Only alpha-numeric characters, dashes and underscores."})

I haven't put a rule in for spaces because it's covered by the third rule, requiring A-Z, a-z, 0-9, _, and -.

Above I'm assuming you want to be as specific as you reasonably can be in the error message. Because it can be done in a single step:

formName.add(Validate.Format,{ pattern: /^[a-zA-Z][a-zA-Z0-9_\-]*$/, failureMessage: "Must start with a letter and consist only of alphanumerics, underscores, or dash characters" } );

(That assumes single-character entries are allowed. If you require at least two characers, change * to +.)




回答3:


You can use this:

/^[a-zA-Z][\w-]*$/


来源:https://stackoverflow.com/questions/17225292/livevalidation-regex-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!