tracman-server/static/js/contact.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-12-12 17:40:07 -07:00
'use strict'
2017-07-04 09:53:01 -06:00
/* global $ */
2018-03-08 19:26:24 -07:00
let validEmail, validMessage
2017-07-04 12:12:54 -06:00
// Validate email addresses
2017-12-12 17:40:07 -07:00
function validateEmail (email) {
2017-12-13 12:52:01 -07:00
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
2017-12-12 17:40:07 -07:00
return re.test(email)
2017-07-04 12:12:54 -06:00
}
// Validate form
2017-12-12 17:40:07 -07:00
function validateForm (input) {
// Check if email is valid
if (input === 'email') {
if (!validateEmail($('#email-input').val())) {
validEmail = false
$('#email-help').show()
$('#submit-button').prop('disabled', true).prop('title', 'You need to enter a valid email address. ')
} else {
validEmail = true
$('#email-help').hide()
validateForm()
}
}
// Ensure message has been entered
if (input === 'message') {
if ($('#message-input').val() === '') {
validMessage = false
$('#message-help').show()
$('#submit-button').prop('disabled', true).prop('title', 'You need to enter a message. ')
} else {
validMessage = true
$('#message-help').hide()
validateForm()
}
// Recheck whole form
} else {
if (validEmail && validMessage) {
$('#submit-button').prop('disabled', false).prop('title', 'Click here to send your message. ')
return true
} else {
$('#submit-button').prop('disabled', true).prop('title', 'Edit the form before clicking send. ')
return false
}
}
2017-07-04 09:53:01 -06:00
}
2017-07-04 12:12:54 -06:00
// Initial check
2017-12-12 17:40:07 -07:00
$(function () {
2017-12-13 12:52:01 -07:00
if (validateEmail($('#email-input').val())) validEmail = true
else validEmail = false
2017-12-12 17:40:07 -07:00
2017-12-13 12:52:01 -07:00
if (!$('#message-input').val() === '')validMessage = true
else validMessage = false
2017-12-12 17:40:07 -07:00
// Use a one-second timout because reCaptcha re-enables the button by default
setTimeout(validateForm, 1000)
})
2017-07-04 12:12:54 -06:00
// Submit form (reCaptcha callback)
2017-12-12 17:40:07 -07:00
window.onSubmit = function () {
2017-12-13 12:52:01 -07:00
if (validateForm()) $('#contact-form').submit()
2017-12-12 17:40:07 -07:00
}
2017-07-04 12:12:54 -06:00
// Form change listener
2017-12-12 17:40:07 -07:00
$('#email-input').change(function () {
validateForm('email')
})
$('#message-input').change(function () {
validateForm('message')
})