Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 54 lines (41 sloc) 1.363 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

jQuery -> # on Document ready

  errorCount = null
  
  valEl = $('.validate') # cache .validate elements
  submitBtn = valEl.parent().parent().find('input[type=submit]')

  valEl.on "blur", ->
    input = $(this)
    validate(input)
  
  validate = (input) ->
    inputVal = input.val()
    inputLen = inputVal.length
    validations = input.data() #cache data-xxx tags

    if validations.notblank is on
      if inputLen <= 0
        throwError("This field can't be blank", input)
      else
        removeError(input)

    if validations.minchars > 0
      if inputLen < validations.minchars
        throwError("You need at least 5 chars", input)
      else
        removeError(input)

    if validations.isinteger is on
      isInteger = /^\d+$/
      if isInteger.test(inputVal) is false
        throwError("Must be a whole number", input)
      else
        removeError(input)

  throwError = (message, input) ->
    if !input.hasClass('has-error')
      errorCount += 1
      input.addClass "has-error"
      input.nextAll('.errorContainer').append(message)

  removeError = (input) ->
    if input.hasClass('has-error')
      errorCount -= 1
      input.removeClass("has-error")
      input.nextAll('.errorContainer').empty()

  submitBtn.on "click", (e) ->
    e.preventDefault()
    if errorCount is 0
      valEl.parent().parent().submit()

Something went wrong with that request. Please try again.