tvalent2
tvalent2

Reputation: 5009

Have CoffeeScript functions return true so I can check them in a click function

I'm using some CoffeeScript functions to perform validations in a form to make sure things are selected or inputs filled in. I'm running the functions once more when the submit button is clicked. If the functions are all true the form can submit. If one is false, I'll call event.preventDefault() on it. Yet with the code I have the page still reloads when the submit button is clicked. It's almost as if the functions aren't returning true or false.

Here's one of my CoffeeScript functions:

checkHSGradYearSelect = ->
  valid = false
  hs_present         = $("#profile_high_school").val() isnt ""
  hs_grad_year_empty = $("#profile_hs_grad_year_1i").val() is ""
  if hs_present and hs_grad_year_empty
    $("#hs_grad label.message").text("High School Grad Year can't be blank")
  else
    $("#hs_grad label.message").text("")
    valid = true
  return valid

I have two others that are very similar.

Here's my click function:

$("form.edit_profile input[type='submit']").click (event) ->
  bday_valid     = $ checkBirthdaySelects
  hs_valid       = $ checkHSGradYearSelect
  highered_valid = $ checkCollegeGradYearSelect
  if !bday_valid  or !hs_valid or !highered_valid
    event.preventDefault()

Can someone help me figure out what I am doing wrong?

UPDATE: If I log bday_valid or hs_valid or highered_valid I get

[document, context: document, constructor: function, init: function, selector: "", jquery: "1.8.3"…]

Upvotes: 0

Views: 277

Answers (1)

pdoherty926
pdoherty926

Reputation: 10379

Remove the $s and add parentheses from your function calls:

bday_valid     = checkBirthdaySelects()
hs_valid       = checkHSGradYearSelect()
highered_valid = checkCollegeGradYearSelect()

Upvotes: 2

Related Questions