Is there a way to reverse the suspension of form submission or is it better not to do this in the first place?

I am trying to create a simple web application. On my login page, I have a form with a text box, password and submit button. Form submission is prevented if both fields are empty. This is the script I use:

function checkLoginCredentials() { var usernameFormValue = $("#usernameForm").val().trim(); var passwordFormValue = $("#passwordForm").val().trim(); var validated; $("#loginForm").submit(function(event){ if (usernameFormValue === "" || passwordFormValue === "") { $("span").html("Enter a username or password"); validated = false } else { validated = true; } return validated; }); } 

However, I noticed that after running the script and submitting the form, the user will no longer be able to log in again. The only alternative I can think of is ALL checks performed by my servlets and utility classes. Is there a way around this or checking for invalid entries, such as empty strings, with my Java classes?

+5
source share
1 answer

The problem is how you assign the verification code. You have checkLoginCredentials , and when you call it, you read the values ​​of the form. And how do you add form submission. You should add reading text field values ​​inside the submit method, and not outside.

 $("#loginForm").submit(function(event){ var usernameFormValue = $("#usernameForm").val().trim(), passwordFormValue = $("#passwordForm").val().trim(), validated; if (usernameFormValue === "" || passwordFormValue === "") { $("span").html("Enter a username or password"); validated = false } else { validated = true; } return validated; }); 
+5
source

Source: https://habr.com/ru/post/1238998/


All Articles