Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

html - Javascript + Spring Boot + Thymeleaf - form validation

I'm trying to validate password and see if they match using JavaScript if passwords don't match show an error, but some for weird reason is like Javascript is not being detected, just submits the wrong fields.

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Reset Your Password</title>

</head>
<body>
<script "text/Javascript">

    document.querySelector('.button').onclick = function (){
        var password = document.querySelector('.password').value,
            confirmPassword = document.querySelector('.confirmPassword').value;

        if(password === ""){
            alert("Field cannot be empty.");
        }
        else if(password !== confirmPassword){
            alert("Password didn't match try again");
        }else if(password === confirmPassword){
            alert("Password match")
        }
    }
</script>
<div class="elelment">
    <h2>Reset Your Password</h2>
    <div class="element-main">
        <h1>Forgot Password</h1>
<form th:action="@{/api/auth/reset_password}" method="post" style="max-width: 350px; margin: 0 auto;">
    <input type="hidden" name="token" th:value="${token}" />
    <div class="border border-secondary rounded p-3">
        <div>
            <p>
                <input type="password" name="password" id="password" class="password"
                       placeholder="Enter your new password"  />
            </p>
            <p>
                <input type="password" class="confirmPassword" id="confirmPassword"  placeholder="Confirm your new password"
                 />
            </p>
            <p class="text-center">
                <input type="submit" value="Change Password" class="button" />
            </p>
        </div>
    </div>
</form>
    </div>
</div>
<div class="copy-right">
    <p>? 2021 Reset Password Form. All rights reserved</a></p>
</div>

</body>
</html>

AM not sure why JavaScript is not being detected and being applied to the form to validate the password fields

question from:https://stackoverflow.com/questions/65661768/javascript-spring-boot-thymeleaf-form-validation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

bro try changing your script tag to this:

    <script th:inline="javascript">
       //your checking conditions
    </script>

Let me know if this works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...