Skip to content

Instantly share code, notes, and snippets.

@paulca
Last active December 29, 2015 12:59
Show Gist options
  • Save paulca/7674241 to your computer and use it in GitHub Desktop.
Save paulca/7674241 to your computer and use it in GitHub Desktop.
jQuery.valitito = {}
jQuery.fn.valid = ->
delete jQuery(@).data()['valid']
jQuery(@).trigger('validate')
if jQuery(@).find('input').length
for input in jQuery(jQuery(@).find('input'))
if !$(input).valid()
jQuery(@).data('valid', false)
break
return true if jQuery(@).data('valid') == undefined
!!jQuery(@).data('valid')
jQuery.fn.validate = (options)->
if typeof options == 'function'
options = {validator: options}
if options?
if options.validator?
jQuery(document).on 'validate', jQuery(@).selector, (e) ->
e.stopPropagation()
jQuery(@).data('valid', options.validator.apply(@))
if options.valid?
jQuery(document).on 'valid', jQuery(@).selector, (e) ->
e.stopPropagation()
options.valid.apply(@)
if options.invalid?
jQuery(document).on 'invalid', jQuery(@).selector, (e)->
e.stopPropagation()
options.invalid.apply(@)
jQuery(document).on 'focusout', jQuery(@).selector, (e)->
return unless jQuery(@).is('input')
jQuery(@).validate()
jQuery(document).on 'keydown', jQuery(@).selector, (e)->
return unless jQuery(@).is('input')
if jQuery(@).valid()
jQuery(@).trigger('valid')
else
jQuery(@).removeClass('pass').removeClass('fail')
else
jQuery(@).trigger('invalid') if !jQuery(@).valid()
jQuery(@).trigger('valid') if jQuery(@).valid()
jQuery(document).on 'submit', jQuery(@).selector, ->
if jQuery(@).valid()
if options.submit
options.submit.apply(@)
else
if options.invalid
options.invalid.apply(@)
false
jQuery.fn.month = ->
parsed_int = parseInt(jQuery(@).val().replace(/^0/, ''))
array_position = jQuery.inArray(parsed_int, [1,2,3,4,5,6,7,8,9,10,11,12])
array_position >= 0
jQuery.fn.year = ->
value = jQuery(@).val()
if "#{value}".length == 2
year = "20#{value}"
else
year = "#{value}"
year.match(/^[0-9]{4}$/) && parseInt(year) >= (new Date()).getFullYear()
jQuery.fn.number = ->
parseInt(jQuery(@).val()) == +jQuery(@).val()
jQuery.fn.present = ->
jQuery.trim(jQuery(@).val()) != ''
jQuery.fn.blank = ->
jQuery.trim(jQuery(@).val()) == ''
jQuery.fn.greater_than = (value) ->
parseInt(jQuery(@).val()) > value
jQuery.fn.email = ->
jQuery(@).val().match(/^[^@]+@[^@]+\.[^@]*[^\.@]{1}$/)
jQuery.fn.less_than = (value)->
if jQuery.trim(null)
value = 0
parseInt(jQuery(@).val()) < value
$('input.form-control.required').validate
validator: ->
$(@).present()
valid: ->
$(@).addClass('pass').removeClass('fail')
invalid: ->
$(@).addClass('fail').removeClass('pass')
@DylanFM
Copy link

DylanFM commented Nov 28, 2013

If you give parseInt a string with a leading 0, it's going to assume it's in base 8. You should pass in 10 as the radix, the second argument to parseInt. More info: https://coderwall.com/p/oyk_ea

Looks like you can skip that for the month & year parseInts though. Well, maybe you'll want it in the year one too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment