如何获取单选按钮的Javascript警报?

时间:2018-07-25 12:02:52

标签: javascript html

<div class="comment">
          <form name="myform" onsubmit="return validateform()">
            <label for="fname">Full Name</label>
            <input type="text" id="fname" name="fullname" placeholder="Your name..">

            <label for="email">E-mail</label>
            <input type="text" id="email" name="email" placeholder="Your e-mail..">

            Rate us<br>
            <input type="radio" id="radio1" name="rate">
            <label for="radio1">Poor</label>
            <input type="radio" id="radio2" name="rate">
            <label for="radio2">Good</label>
            <input type="radio" id="radio3" name="rate">
            <label for="radio3">Excelent</label>

            <br>
            <label for="subject">Comments</label>
            <textarea id="subject" name="subject" placeholder="Write something.." style="height:200px"></textarea>

            <input type="submit" value="Submit">
          </form>
        </div>

        <script type="text/javascript">
            function validateform(){
                var name=document.forms["myform"]["fname"].value;
                var poor = document.getElementById("radio1").checked;
            var good = document.getElementById("radio2").checked;
            var excelent = document.getElementById("radio3").checked;
            var text = document.forms["myform"]["subject"].value;

                if (name == "" || name==null || !name.match(/^[a-zA-Z]+$/)) {
                    alert("Please enter name");
                    return false;
                }
                if (text == "" || text==null || !text.match(/^[a-zA-Z]+$/)) {
                    alert("Please enter valid text");
                    return false;
                }
                else if (poor==true) {
                alert(" Dear "  + name +    " , We appriciate your feedback. You have rated our site as POOR and your comment was " + text);
            return false;
            }
            else if (good==true) {
                alert(" Dear "  + name +    " , We appriciate your feedback. You have rated our site as GOOD and your comment was " + text);
            return false;
            }
                else if (excelent==true) {
                alert(" Dear "  + name +    " , We appriciate your feedback. You have rated our site as EXCELENT and your comment was " + text);
            return false;
            }

            }
        </script>

这是我正在创建的注释部分代码的一部分。我已经完成了名称和文本区域的验证,但是我找不到找到警告消息的方法, “尊敬的(姓名)。我们感谢您的反馈。您已将我们的网站评为(相关的单选按钮名称),您的评论为(对评论区域的评论)。” 。有人可以告诉我如何显示此警报消息。

(无需验证电子邮件)

3 个答案:

答案 0 :(得分:1)

<input type="email" id="email" name="email" placeholder="Your e-mail..">

有一个input type='email',如果提交,它将检查这是否是有效的电子邮件。

请参见documentation here

答案 1 :(得分:0)

我知道这并不是您真正要求的,但这是执行验证的一种更加防错的方法,并且确实易于使用。 https://validatejs.org/,检查出来。

答案 2 :(得分:-1)

我找到了电子邮件地址的模式=>  / ^ [a-z0-9 ._%+-] + @ [a-z0-9 .-] +。[a-z] {2,4} $ /;

我不知道输入的文本区域是什么,但是您可以像在这里那样检查它是否已填充=> 名称==“”