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

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-10-02 16:59:30 +02:00
/* globals $ */
2016-06-06 16:17:19 +02:00
2018-10-02 16:59:30 +02:00
$(document).on('turbolinks:load', action_type_de_champs);
2016-06-06 16:17:19 +02:00
function action_type_de_champs() {
2018-10-02 16:59:30 +02:00
$("input[type='email']").on('change', function() {
2017-04-04 16:15:33 +02:00
toggleErrorClass(this, validateEmail($(this).val()));
});
2016-06-06 16:17:19 +02:00
2018-10-02 16:59:30 +02:00
$("input[type='number']").on('change', function() {
2017-04-04 16:15:33 +02:00
toggleErrorClass(this, validateNumber($(this).val()));
});
2018-10-02 16:59:30 +02:00
$("input[type='phone']").on('change', function() {
var val = $(this).val();
2017-04-04 16:15:33 +02:00
val = val.replace(/[ ]/g, '');
2016-06-06 16:17:19 +02:00
2017-04-04 16:15:33 +02:00
toggleErrorClass(this, validatePhone(val));
});
2016-06-06 16:17:19 +02:00
}
function toggleErrorClass(node, boolean) {
2018-10-02 16:59:30 +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}$/;
2018-10-02 16:59:30 +02:00
return validateInput(phone, re);
2016-06-06 16:17:19 +02:00
}
function validateEmail(email) {
2018-10-02 16:59:30 +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]+$/;
2018-10-02 16:59:30 +02:00
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);
}