I would like to sanitize the form fields before sending the ajax request to increase security. Currently my Javascript code is this:
jQuery(document).ready(function($) {
$('#login-form').submit(function(e) {
e.preventDefault(); // stop the form from submitting the normal way
var form = $(this);
var data = {
'action': 'login',
'username': form.find('#username').val(),
'password': form.find('#password').val(),
'remember': form.find('#remember').val(),
'nonce': form.find('input[name="nonce"]').val()
};
$.ajax({
type: 'POST',
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
data: data,
success: function(response) {
if (response.success) {
location.reload();
} else {
$('#login-form-message').html(response.data.message);
}
}
});
});
});
I'm trying to sanitize the input fields like this, I'm a beginner and I'm not entirely sure what I'm doing. Can anyone provide a tip? I appreciate any response, thanks.
var sanitize = require('sanitize-html');
jQuery(document).ready(function($) {
$('#login-form').submit(function(e) {
e.preventDefault(); // stop the form from submitting the normal way
var form = $(this);
var username = sanitize(form.find('#username').val());
var password = sanitize(form.find('#password').val());
var remember = sanitize(form.find('#remember').val());
var nonce = sanitize(form.find('input[name="nonce"]').val());
var data = {
'action': 'login',
'username': username,
'password': password,
'remember': remember,
'nonce': nonce
};
$.ajax({
type: 'POST',
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
data: data,
success: function(response) {
if (response.success) {
location.reload();
} else {
$('#login-form-message').html(response.data.message);
}
}
});
});
});