E
E
Evgeny Petryaev2019-10-19 00:05:42
JavaScript
Evgeny Petryaev, 2019-10-19 00:05:42

Why does submit happen?

Page:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Create User</title>
<link rel="stylesheet" href="create.css">
<script type="text/javascript" src="create.js"></script>
</head>
<body>
<h1>Create Account</h1>
<p id="message" >Enter your information</p>
<div id="CenterBlock">
<form method="post">
<label class="labeler">NickName</label><br>
<input class="inputer" name="nickname"/><br><br>
<label class="labeler">Password</label><br>
<input class="inputer" name="password" type="password" /><br><br>
<label class="labeler">Email</label><br>
<input class="inputer" id="email" name="email" /><br><br>
<input class="button" type="submit" value="Save" onsubmit="return ValidMail()" />
</form>
</div>
</body>
</html>

create.js:
function ValidMail() {
    var re = /^[\w-\.][email protected][\w-]+\.[a-z]{2,4}$/i;
    var myMail = document.getElementById('email').value;
    var valid = re.test(myMail);
    if (valid) output = 'Адрес эл. почты введен правильно!';
    else output = 'Адрес электронной почты введен неправильно!';
    document.getElementById('message').innerHTML = output;
    return valid;
}

There is an entry in the database of an empty user and redirection to the main page when entering empty values
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author User
 */
@WebServlet("/create")
public class CreateServlet extends HttpServlet{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        getServletContext().getRequestDispatcher("/create.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
        try {
            String nickname = request.getParameter("nickname");
            String  email = request.getParameter("email");
            String  password = request.getParameter("password");
            Users users = new Users(nickname,email, password);
            UsersDB.insert(users);
            response.sendRedirect(/*request.getContextPath()+*/"/index.html");
        }
        catch(Exception ex) {
            getServletContext().getRequestDispatcher("/create.jsp").forward(request, response); 
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Karo, 2019-10-19
@Zraza

The submit event does not occur on the button, but on the form
Correctly like this:
<form method="post" onsubmit="return ValidMail()">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question