email validation function

R

roohbir

Hello,
I need help with the following code. Would be very grateful for
comments.
I have a function called emailValid, and it is supposed to alert the
user if h/she leaves the field blank. I want to run this function
before the ComputeCost function but don't know how to go about doing
it.
Thanks in advance.
Roohbir

<html>
<head>
<script type="text/javascript" src="myOwnJavaScript.js">
</script>
</head>

<body>
<script type="text/javascript">

function emailValid()
{
email = sampleform.text2.value
if (email == "") {
alert("Invalid email address") // cannot be empty
return false
}

return true
}

function ComputeCost()
{
//create an object
obj = new ShippingInfo(sampleform.menu1.value,
sampleform.menu2.value, sampleform.text1.value, sampleform.text2.value)

//add a method
obj.GetShippingCost=GetShippingCost;

//Invoke a method.
var Cost = obj.GetShippingCost();
var message = document.getElementById("myOwndivElement");
message.innerHTML = "Cost = " + Cost;
return false;

}
</script>
<form name="sampleform" onsubmit="ComputeCost();return false;">

<p>Shipping Destination:
<select name="menu1">
<option value="">--Select a destination--</option>
<option value="Boston">Boston</option>
<option value="Seoul">Seoul</option>
</select>


<p>Shipping Type:
<select name="menu2">
<option value="">--Select a delivery method--</option>
<option value="Express Delivery">Express Delivery</option>
<option value="Standard Delivery">Standard Delivery</option>
</select>


<p>Receiver:
<input name="text1" type="text" size="20"></input>

<p>Receiver's Email Address:
<input name="text2" type="text" size="20"
onchange="emailValid();"></input>

<p><input type="submit" value="Submit">

<div id="myOwndivElement"></div>
</form>
</body>
</html>
 
W

webEater

roohbir said:
Hello,
I need help with the following code. Would be very grateful for
comments.
I have a function called emailValid, and it is supposed to alert the
user if h/she leaves the field blank. I want to run this function
before the ComputeCost function but don't know how to go about doing
it.
Thanks in advance.
Roohbir

<html>
<head>
<script type="text/javascript" src="myOwnJavaScript.js">
</script>
</head>

<body>
<script type="text/javascript">

function emailValid()
{
email = sampleform.text2.value
if (email == "") {
alert("Invalid email address") // cannot be empty
return false
}

return true
}

function ComputeCost()
{
//create an object
obj = new ShippingInfo(sampleform.menu1.value,
sampleform.menu2.value, sampleform.text1.value, sampleform.text2.value)

//add a method
obj.GetShippingCost=GetShippingCost;

//Invoke a method.
var Cost = obj.GetShippingCost();
var message = document.getElementById("myOwndivElement");
message.innerHTML = "Cost = " + Cost;
return false;

}
</script>
<form name="sampleform" onsubmit="ComputeCost();return false;">

<p>Shipping Destination:
<select name="menu1">
<option value="">--Select a destination--</option>
<option value="Boston">Boston</option>
<option value="Seoul">Seoul</option>
</select>


<p>Shipping Type:
<select name="menu2">
<option value="">--Select a delivery method--</option>
<option value="Express Delivery">Express Delivery</option>
<option value="Standard Delivery">Standard Delivery</option>
</select>


<p>Receiver:
<input name="text1" type="text" size="20"></input>

<p>Receiver's Email Address:
<input name="text2" type="text" size="20"
onchange="emailValid();"></input>

<p><input type="submit" value="Submit">

<div id="myOwndivElement"></div>
</form>
</body>
</html>

I have a function in PHP to do this, you have to do it server side
again, only in this case it's really secure:

function validMail($data, $strict=false)
{
$regex = $strict ?
'/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' :
'/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
return preg_match($regex, trim($data));
}

I think it's easy to port this function to JS, else ask me again.

Andi
 
R

roohbir

Actually, I don't need Server side coding here because the function
will alert the user if the user fails to enter anything in the email. I
just wanted to know how I can use this function before the ComputeCost
function. I mean, on submitting the form, till now the application's
event handler was ComputeCost.
Hope this explains it a little bit.
Cheers
Roohbir

roohbir said:
Hello,
I need help with the following code. Would be very grateful for
comments.
I have a function called emailValid, and it is supposed to alert the
user if h/she leaves the field blank. I want to run this function
before the ComputeCost function but don't know how to go about doing
it.
Thanks in advance.
Roohbir
<html>
<head>
<script type="text/javascript" src="myOwnJavaScript.js">
</script>
</head>
<body>
<script type="text/javascript">
function emailValid()
{
email = sampleform.text2.value
if (email == "") {
alert("Invalid email address") // cannot be empty
return false
}
return true
}
function ComputeCost()
{
//create an object
obj = new ShippingInfo(sampleform.menu1.value,
sampleform.menu2.value, sampleform.text1.value, sampleform.text2.value)
//add a method
obj.GetShippingCost=GetShippingCost;
//Invoke a method.
var Cost = obj.GetShippingCost();
var message = document.getElementById("myOwndivElement");
message.innerHTML = "Cost = " + Cost;
return false;
}
</script>
<form name="sampleform" onsubmit="ComputeCost();return false;">
<p>Shipping Destination:
<select name="menu1">
<option value="">--Select a destination--</option>
<option value="Boston">Boston</option>
<option value="Seoul">Seoul</option>
</select>
<p>Shipping Type:
<select name="menu2">
<option value="">--Select a delivery method--</option>
<option value="Express Delivery">Express Delivery</option>
<option value="Standard Delivery">Standard Delivery</option>
</select>
<p>Receiver:
<input name="text1" type="text" size="20"></input>
<p>Receiver's Email Address:
<input name="text2" type="text" size="20"
onchange="emailValid();"></input>
<p><input type="submit" value="Submit">
<div id="myOwndivElement"></div>
</form>
</body>
</html>I have a function in PHP to do this, you have to do it server side
again, only in this case it's really secure:

function validMail($data, $strict=false)
{
$regex = $strict ?
'/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' :
'/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
return preg_match($regex, trim($data));

}I think it's easy to port this function to JS, else ask me again.

Andi
 
D

Danny

Well, the regex below will cover emails with alphanumeric ones including -
and ., you can add more if you wish though barely necessary most likely :) .



<form name="sampleform" onsubmit="return ComputeCost()">

<input name="text2" type="text" size="20" onchange="emailValid(this);">

---------------------------

function emailValid(el)
{
em=/[\w-\.]+@[\w-\.]+[a-z]+/i;
if (!em.test(el.value)) {
alert('yo buddy, enter a correct email please');
el.select();
}
}


Danny
 
P

Pedro Graca

roohbir said:
I have a function called emailValid, and it is supposed to alert the
user if h/she leaves the field blank. I want to run this function
before the ComputeCost function but don't know how to go about doing
it.
Thanks in advance.
Roohbir

<html>
<head>
<script type="text/javascript" src="myOwnJavaScript.js">
</script>
</head>

<body>
<script type="text/javascript">

function CallTwoFunctions()
{
if (emailValid()) ComputeCost();
}
function emailValid()
{
}

function ComputeCost()
{
}
</script>
<form name="sampleform" onsubmit="ComputeCost();return false;">

<form name="sampleform" onsubmit="CallTwoFunctions();return false;">

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top