Wednesday 15 June 2011

javascript - Livevalidation - Regex issue -


I am using LiveValidation () to validate a form for a page URL on my site.

  • can not be empty
  • should start with a letter
  • no spaces are allowed
  • alpha-numeric Characters, Dashes and Underscores

    So far I have this:

      var formName = New LiveValidation ("Site Page-Name"); FormName.add (Validate.Presence); FormName.add (valid.format, {pattern: / ^ [a-zA-Z] /, Failure message: "must start with a letter."}); FormName.add (valid.Exclusion, {within: [']], partial match: true, failure message: "Spaces are not allowed."}); FormName.add (Validate.Format, {pattern: / [a-zA-Z0-9 -_] + $ /, failure message: "alpha-numeric characters, dashes and underscores only."})   

    The first and second conditions are met I am struggling with the third and fourth conditions of "empty space" and "only alpha-numeric characters, dashes and underscores"

    I tried to do the space and the fourth position with Reggae but it was not working, so I have now separated the empty space for now. The pattern works for the fourth position if the character you type is not the same in the regedx pattern. But if you type "valid" characters and first keep the "invalid" character, then the error is removed and it becomes valid.

    Do anyone know how to hold the pattern properly, if any input box has "non-valid" characters? It would also be nice if I could not merge any position status with patterns if possible.

    / ^ [one-zA-Z] [a-zA-Z0- 9 _ \ -] * $ / .

    • [one-zA-Z] means the letter.
    • [a-zA-Z0- 9 _ \ -] means letters, numbers, dashes or underscores.
    • * means repeat 0 or more times
    • ^ is the beginning of the string
    • $ is the end of the string

      It reads: At the beginning of the string, match later, then match zero or more letters, digits or underscore characters, then Match the end of the string.

  • No comments:

    Post a Comment