检查提交的电子邮件格式是否正确 [英] Cheking if form of submited email is correct

查看:110
本文介绍了检查提交的电子邮件格式是否正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了此脚本来检查电子邮件形式,用户将进入文本框

I've created this script to check form of email user will enter into textbox

function checkEmail() {

  var mail = document.getElementById('mail');
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; /* regex        koda najdena na codeproject.com*/

  if (!filter.test(mail.value)) 
{
    alert('enter valid email');
    mail.focus;
    return false;
}

它用于检查此脚本中的表单,并在显示之前提醒您您可以继续

It's used to check form in this script and suposed to show you alert, before you are able to continue

<?php
    $flag = 0;
    if(isset($_POST['submit']))
    {   
        $con = mysql_connect("localhost","root","");
        if (!$con){
          die("cannot connect: " . mysql_error());
        }

        mysql_select_db("strek", $con); 

        $nar_db="SELECT * FROM narocniki;";  
        $result = mysql_query($nar_db, $con);

        while($row=mysql_fetch_array($result))
        {
            if($_POST['mail']==$row['mail'])
                $flag = 1;
        }
        if($flag==0)
        {
            $sql = "INSERT INTO narocniki (mail)  VALUE ('$_POST[mail]')"; 
            mysql_query($sql, $con);    
            mysql_close($con);
            ?>
              <p align="center" class="vsebina" >
                Tvoj mail je bil uspešno sprejet!</p>    
            <p align="center"><a href="dogodki.php"><input type="button" name="return" value="nazaj"></a></p>
            <?php
        }
        else
        {
            echo '<p align="center" class="vsebina">Naveden mail je že v bazi naročnikov!</p>';
            ?>
            <p align="center"><a href="dogodki.php"><input type="button" name="return" value="nazaj"></a></p>
            <?php
        }
    }
    else
    {
        include("vsebina/tabdog.html");
    ?>
         <p align="center" class="vsebina" id="mail">
      naroči se na najnovejše novice</p>

        <form action="dogodki.php" method="post">
            <p align="center" class="vsebina">vnesi svoj e-naslov:  <input type="text" name="mail" id="mail" required>

                <input type="submit" name="submit" value="potrdi" onClick="return checkEmail()">  

            </p>
        </form>
    <?php
    }
  ?>

可能只是缺少
我应该只在代码中包含脚本,以及在哪里最好直接将其放置在头部,或者将其放置在头部之间

It's probably just something missing Should I rather just include script inside the code, and where would be the best to place it-weather directly in head or rather somewhere in between

在我的代码中甚至可能这样做,因为它已经检查数据库中是否存在邮件,然后还需要检查电子邮件的形式

Is this even possible in my code, because it already checks if mail exists in database, and would then also need to check the form of email

推荐答案

而不是在 onclick 提交按钮的属性,请尝试在表单的 onsubmit 属性中进行操作:

Instead of doing it in the onclick attribute of the submit button, try doing it in the onsubmit attribute of the form:

<form action="dogodki.php" method="post" onsubmit="return checkEmail()">

这篇关于检查提交的电子邮件格式是否正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆