Thursday, 5 September 2013

Cheking if form of submited email is correct

Cheking if form of submited email is correct

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

No comments:

Post a Comment