lbry.com/web/js/yt2/FormValidation.js
2020-03-04 19:06:33 +01:00

97 lines
3.1 KiB
JavaScript

var is_first_time = true;
function submitEditForm() {
$("#youtube_settings").submit(function (event) {
// get value from id
var lbry_channel_name = $.trim($('#channel-name').val());
var email = $.trim($('#email').val());
// Hide the error message
$('#channel-name-error').hide();
$('#email-error').hide();
// If the channel name is invalid or blank stop the post request
if (!validateLBRYName(lbry_channel_name) || lbry_channel_name === '') {
$('#lbry_channel_name').addClass('error_form');
$('#channel-name-error').show();
event.preventDefault();
}
if (!validateEmail(email) || email === '') {
$('#email').addClass('error_form');
$('#email-error').show();
event.preventDefault();
}
else if (!validateEmailIsNotGooglePlus(email)) {
$('#email').addClass('error_form');
if (is_first_time) {
$('#email').addClass('error_form');
$('#email-google-plus-error').show();
is_first_time = false;
event.preventDefault()
}
}
localStorage.setItem("status_token", $.trim($('#status_token').val()));
localStorage.setItem("lbry_channel_name_sync", $.trim($('#channel-name').val()));
});
}
function resendVerificationEmail(token, email) {
let data = new FormData();
data.append("status_token", $.trim($('#status_token').val()));
data.append("email", $.trim($('#email').val()));
let xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://localhost:8080/yt/resend_verification_email");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Accept", "*/*");
xhr.send(data);
}
function submitDetailsForm() {
$("#youtube_claim").submit(function (event) {
// get value from id
var lbry_channel_name = $.trim($('#lbry_channel_name').val());
// Make sure that the error message are hidden before trying to validate value
$('#lbry_error').hide();
// If the lbry name is invalid or blank stop the post request
if (!validateLBRYName(lbry_channel_name) || lbry_channel_name === '') {
$('#lbry_channel_name').addClass('error_form');
$('#lbry_error').show();
event.preventDefault();
}
});
}
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}
function validateLBRYName(lbry_channel_name) {
var re = /^[@A-Za-z0-9-]*$/g;
return re.test(lbry_channel_name);
}
function validateYoutubeChannelUrl(youtube_channel_url) {
var re = /^UC[A-Za-z0-9_-]{22}$/;
return re.test(youtube_channel_url);
}
function validateEmailIsNotGooglePlus(email) {
var re = /^[A-Za-z0-9._%+-]+@(?!plusgoogle.com)[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
return re.test(email);
}