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>
webEater - 31 Oct 2006 05:22 GMT
> Hello,
> I need help with the following code. Would be very grateful for
[quoted text clipped - 72 lines]
> </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
roohbir - 31 Oct 2006 06:19 GMT
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
> > Hello,
> > I need help with the following code. Would be very grateful for
[quoted text clipped - 84 lines]
>
> Andi
Danny - 31 Oct 2006 07:05 GMT
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
Pedro Graca - 31 Oct 2006 11:17 GMT
> 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
[quoted text clipped - 11 lines]
> <body>
> <script type="text/javascript">
function CallTwoFunctions()
{
if (emailValid()) ComputeCost();
}
> function emailValid()
> {
<snip>
> }
>
> function ComputeCost()
> {
<snip>
> }
> </script>
> <form name="sampleform" onsubmit="ComputeCost();return false;">
<form name="sampleform" onsubmit="CallTwoFunctions();return false;">
<snip>

Signature
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.