demarches-normaliennes/app/assets/javascripts/old_design/description.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-03-30 17:00:35 +02:00
$(document).on('turbolinks:load', action_type_de_champs);
2016-06-06 16:17:19 +02:00
function action_type_de_champs() {
2017-04-04 16:15:33 +02:00
$("input[type='email']").on('change', function () {
toggleErrorClass(this, validateEmail($(this).val()));
});
2016-06-06 16:17:19 +02:00
2017-04-04 16:15:33 +02:00
$("input[type='number']").on('change', function () {
toggleErrorClass(this, validateNumber($(this).val()));
});
2017-04-04 16:15:33 +02:00
$("input[type='phone']").on('change', function () {
val = $(this).val();
val = val.replace(/[ ]/g, '');
2016-06-06 16:17:19 +02:00
2017-04-04 16:15:33 +02:00
toggleErrorClass(this, validatePhone(val));
});
2017-04-04 16:15:33 +02:00
address_type_init();
2016-06-06 16:17:19 +02:00
}
function toggleErrorClass(node, boolean) {
2017-04-04 16:15:33 +02:00
if (boolean)
$(node).removeClass('input-error');
else
$(node).addClass('input-error');
2016-06-06 16:17:19 +02:00
}
function validatePhone(phone) {
2017-04-04 16:15:33 +02:00
var re = /^(0|(\+[1-9]{2})|(00[1-9]{2}))[1-9][0-9]{8}$/;
return validateInput(phone, re)
2016-06-06 16:17:19 +02:00
}
function validateEmail(email) {
2017-04-04 16:15:33 +02:00
var 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,}))$/;
return validateInput(email, re)
2016-06-06 16:17:19 +02:00
}
function validateNumber(number) {
2017-04-04 16:15:33 +02:00
var re = /^[0-9]+$/;
return validateInput(number, re)
}
2016-06-06 16:17:19 +02:00
function validateInput(input, regex) {
2017-04-04 16:15:33 +02:00
return regex.test(input);
}