简体   繁体   English

发送和显示错误的消息

[英]Wrong messages being sent and displayed

I have a problem with my JavaScript code. 我的JavaScript代码有问题。 I've been trying to fix it for a really long time now but since i am a beginner at JavaScript, im kinda lost on my own... First of all, a hidden message should be showing when you submit the page without filling in any fields. 我已经尝试修复很长时间了,但是由于我是JavaScript的初学者,因此我自己就迷失了……首先,当您提交页面而未填写时,应该显示隐藏消息。任何领域。 Second, when i click a radio button and check some checkboxes and then submit that, my alarm still goes off for some reason. 其次,当我单击一个单选按钮并选中一些复选框然后提交时,由于某种原因,我的警报仍然响起。 I really desperately need help with this, but i have noone to ask except you guys so i really hope you can help me! 我真的非常需要这个方面的帮助,但是除了你们之外,我没人问,所以我真的希望您能帮助我! Here is are the codes: HTML 以下是代码: HTML

<body>
<div id="case">
    <div id="title">
    <h1>Anmeldung</h1>
    </div>
    <div id="formulare">
        <form onsubmit="checkPw(this)">
            <div id="benutzerdaten">
                <h3>Benutzerdaten</h3>
                <p id="general_alarm" class="alarm">Bitte füllen Sie alle benötigten Felder aus!</p>

                <label for="Benutzername">
                <input type="email" name="username" id="username" placeholder="Benutzernamen" required>
                <p id="user_alarm" class="alarm">Bitte geben Sie eine gültige E-mail Adresse ein!</p>

                <label for="Passwort">
                <input type="Password" name="password1" id="first_pw" placeholder="Passwort" required>
                <p id="password1_alarm" class="alarm">Das Passwort muss mindestens 9 Zeichen lang sein und muss Gross und Kleinbuchstaben und Sonderzeichen enthalten<br> ( ) [ ] { } ? ! $ % &amp / = * + ~ , . ; : < > - </p>

                <label for="Passwort">
                <input type="Password" name="password2" id="second_pw" placeholder="Passwort wiederholen"required>
                <p id="password2_alarm" class="alarm">Das eingegebene Passwort stimmt nicht mit dem ersten überein!</p>
            </div> 
        </form>

        <form>
            <div id="altersgruppe">
                <h3>Altersgruppe</h3>
                <input type="radio" id="opt1" name="Alter" value="<20"><label id="radio1" for="opt1"> &lt;20</label>

                <input type="radio" id="opt2" name="Alter" value="20-39"><label id="radio2" for="opt2"> 20-39</label>

                <input type="radio" id="opt3" name="Alter" value="40-60"><label id="radio3" for="opt3"> 40-59</label>

                <input type="radio" id="opt4" name="Alter" value=">60"><label id="radio4" for="opt4"> >60</label>

                <p id="radio_alarm" class="alarm">Bitte geben Sie ihr Alter an!</p>
            </div>
        </form>

        <form>
            <div id="programmiersprache">
                <h3>Vorgezogene Programmiersprache</h3>
                <h4>Mehrere Auswahlen möglich</h4>
                <label for="check1">
                <input type="checkbox" name="Programmiersprache" value="C++" id="check1">C++</label> 

                <label for="check2">
                <input type="checkbox" name="Programmiersprache" value="C#" id="check2">C#</label>  

                <label for="check3">
                <input type="checkbox" name="Programmiersprache" value="C" id="check3">C</label> 

                <label for="check4">
                <input type="checkbox" name="Programmiersprache" value="Java" id="check4">Java</label>

                <p id="checkbox_alarm" class="alarm">Bitte geben Sie mindestens eine Programmiersprache an!</p>
            </div>
        </form>

        <form>
            <div id="betriebssystem">
                <h3>Betriebssystem</h3>
                <select>
                    <option value="Windows" name="Betriebssystem">Windows</option>
                    <option value="Mac OS" name="Betriebssystem">Mac OS</option>
                    <option value="Linux" name="Betriebssystem">Linux</option>

                    <p id="option_alarm" class="alarm">Bitte wählen Sie ein Betriebssystem aus!</p>
                </select>
            </div>
        </form>

            <div id="reset/submit">
            <button type="button" id="reset">Eingaben zurücksetzen</button>
            <button type="button" id="submit">Eingaben absenden</button>
            </div>

        </form>
    </div>
</div>

JavaScript JavaScript的

$(document).ready(function(){

//versteckt alarme
$('#password1_alarm').hide();
$('#password2_alarm').hide();
$('#user_alarm').hide();
$('#radio_alarm').hide();
$('#checkbox_alarm').hide();
$('#general_alarm').hide();

//pattern für username und passwort definieren
var username_pattern = /.+@.+[.].+/;
var password_pattern = [/[a-zäöü]+/, /[A-ZÄÖÜ]+/, /[0-9]+/, /[\(\)\[\]\{\}\?\!\$\%\&\/\=\*\+\~\,\.\;\:\<\>\-\_]+/];

//überprüft passwort
function checkPw(password1) {
        if (password1.length < 9) {
            $("#password1_alarm").show();
        } else {
            for (var i = 0; i < password_pattern.length; i++) {
                if (!password_pattern[i].test(password1)) {
                    $("password1_alarm").show();
                    return false;
                } else {
                    $("password1_alarm").hide();
                    if (password_pattern.length -1 == i) {
                        return true;
                    }
                }
            }
        }
    };

//überprüft email 
function checkUn(username){
    if (!username_pattern.test(username)) {
        $('#user_alarm').show();
        return false;
    } else {
        $('#user_alarm').hide();
        return true;
    }
}

$('#first_pw').on("keyup", function(){
    var password1 = $("#first_pw").val();
    checkPw(password1);
});

// Hier wird direkt bei der Eingabe geprüft ob das zweite Passwortfeld 9 Zeichen lang ist und alle vorgegebenen Zeichen ethält.
$('#second_pw').on("keyup", function(){
    var password2 = $("#second_pw").val();
    checkPw(password2);
});

// Hier wird direkt bei der Eingabe geprüft ob der Username einer Email entspricht.
$('#username').on("keyup", function(){
    var username = $('#username').val();
    checkUn(username);
});

$("#submit").click(function(){
    var username = $("#username").val();
    var first_pw = $("#first_pw").val();
    var second_pw = $("#second_pw").val();
    var radio = $('input[name=Alter]:checked', '#radio').val();
    var country = $(".checkbox:selected").val();
    var checkbox = [];

    $(".checkbox").each(function(){
        if ($(this).prop("checked")) {
            checkbox.push($(this).val());
        }
    });

    if (username && first_pw && !second_pw && !radio && !checkbox.length) {
        var password1 = $("#first_pw").val();
        var username = $('#username').val()
        if (checkUn(username) && checkPw(password1)) {
            window.location.href = "login.html?username=" + username;
        }
    } else if (!username && !first_pw && !second_pw) {
        $('general_alarm').show();
    } else {
        $('general_alarm').hide();
        var pw_ok = false;
        var opt_ok = false;
        var chbx_ok = false;

        if (first_pw != second_pw) {
            $('#password2_alarm').show();
            var pw_ok = false;
        } else {
            $('#password2_alarm').hide();
            var pw_ok = true;
            var password = $("#first_pw").val();
        }

        if (!radio) {
            $('#radio_alarm').show();
            var opt_ok = false;
        } else {
            $('#radio_alarm').hide();
            var opt_ok = true;
        }

        if (!checkbox.length) {
            $('#checkbox_alarm').show();
            var chbx_ok = false;
        } else {
            $('#checkbox_alarm').hide();
            var chbx_ok = true;
        }

        if (checkUn(username) && checkPw(password) && pw_ok && opt_ok && chbx_ok){
            window.location.href = "anmeldung.html?username="+username+"&password="+password1+"&radio="+radio+"&checkbox="+checkbox+"&country="+country+"";
        }
    }
});

$("#reset").click(function(){
    if (confirm("Sind Sie sicher das Sie all Ihre Daten zurücksetzen wollen?")) {
        document.location.reload(true)
    }
});

}); });

You seem to be doing a lot of unnecessary checks. 您似乎在做很多不必要的检查。 For example, you're checking usernames and passwords on both 'keyup' and when the user submits the form. 例如,您正在检查“ keyup”上以及用户提交表单时的用户名和密码。 I would suggest this: streamline your checks so that you're only testing each field once. 我建议这样做:简化检查,以便只对每个字段进行一次测试。 I think you're getting into some weird logic flow when you're checking for !checkbox.length several times in the same click event. 我认为在同一单击事件中多次检查!checkbox.length时,您会陷入一些奇怪的逻辑流程。 For example, psuedo code like this might be better: 例如,像这样的伪代码可能更好:

//simple check username function
function checkUn(username){
    if (!username_pattern.test(username)) return false;
}

var $username_ok = false; //plus all other 'ok' bool variables.
$('#username').blur(function(){
    //check username
    if(CheckUn($(this).val())) $username_ok = true;
    //don't need else, since $username_ok is already false at declaration
});

//do same for password (include length check) and all other fields
//then, on submit:

$('#submit').click(function(e){
    e.preventDefault();
    if(!username_ok && !password_ok && ... ){
    //display general alarm here
    return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发送消息时未调用WebSocket onmessage() - WebSocket onmessage() not being called when messages are sent 为什么用逗号显示数字的位置错误? - Why is the Number being Displayed with Comma is wrong place? 此代码有什么问题,标记未显示在 map 中? - What is wrong on this code that the marker is not being displayed in the map? Twilio IP消息系统:在显示已发送消息之前一秒的延迟 - Twilio IP Messaging : one sec latency before sent messages be displayed AJAX数据被发送到错误的Django视图 - AJAX data being sent to the wrong Django view 按用户和期限清除消息时,发送了错误的嵌入 - When purging messages by user and term, the wrong embed is sent Ratchet PHP Websockets:私人消息传递(控制消息发送给谁) - Ratchet PHP Websockets: Private messaging (control who messages are being sent to) 并发jquery jsonp响应被发送到错误的成功处理程序 - Concurrent jquery jsonp response being sent to wrong success handler 当我单击网页上的文本框时,为什么我的消息没有显示? - Why are my messages not being displayed when I click out of the textbox on my webpage? 如何创建多个子分支并在子进程和主进程之间发送多个消息 - How to create multiple child forks and have multiple messages being sent between child processes and main process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM