Skip to main content
Known Participant
July 7, 2008
Question

Javascripit edit in existing form

  • July 7, 2008
  • 1 reply
  • 290 views
I have a cfform with javascript edits for radio buttons, and required fields. My existing code is below and everything works fine :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<script language="javascript">

function validateForm(buyer_form){

var returnStatus = true;

myOption = -1;
for (i=buyer_form.reviewStatus.length-1; i > -1; i--) {
if (buyer_form.reviewStatus .checked) {
myOption = i; i = -1;
}
}
if (myOption == -1) {
alert("Please indicate whether you wish to send a letter or not.");
return false;
}

if ((buyer_form.reviewStatus[0].checked) && (buyer_form.supplier_contact_name.value == "")){
alert("Please enter the supplier contact name.");
buyer_form.supplier_contact_name.focus();
returnStatus(false);
}

if ((buyer_form.reviewStatus[0].checked) && (buyer_form.supplier_email.value == "")){
alert("Please enter the supplier email address.");
buyer_form.supplier_email.focus();
returnStatus(false);
}


if ((buyer_form.reviewStatus[1].checked) && (buyer_form.nor_comments.value == "")){
alert("Please describe why you are not sending a NOR to this supplier.");
buyer_form.nor_comments.focus();
returnStatus(false);
}

if (returnStatus){
buyer_form.submit();
}

returnStatus(true);
}

</script>

<body class="content">

<cfform name="buyer_form" method="post" action="test_email.cfm">

<center>
<table border="0" width="760">
<tr>
<td valign="top">
<table border="0" width="380">
<tr>
<td align="right"><cfinput type="radio" name="reviewStatus" value="Approved">Send:</td>
<td align="center" valign="bottom">
<font size="1"><b><i>(Vice President of Sales or Supply Chain)</i></b></font></td>
</tr>
<tr>
<cfoutput>
<td align="right">Contact Name:</td><td align="center">
<cfinput type="text" name="supplier_contact_name" size="40" maxlength="40"></td>
</tr>
<tr>
<td align="right" valign="top">Supplier e-mail:</td>
<td align="center" valign="top">
<cfinput type="text" name="supplier_email" size="40" maxlength="200">
</td>
</tr>
</cfoutput>
</table>
</td>
<td valign="top">
<table border="0" width="380">
<tr>
<td valign="top">
<cfinput type="radio" name="reviewStatus" value="Disapproved"> Do Not Send due to the following issue(s):
</tr>
<tr>
<td><textarea name="nor_comments" rows="8" cols="40"></textarea></td>
</tr>
</table>
</td>
</tr>
</table>
</center>
<p> 
<center>
<table border="0" widht="760">
<tr>
<td colspan="3" align="center"><hr width="760" size="2" color="black" align="center"></td>
</tr>
<tr>
<td>
<input type="button" id="submitButton" value="Submit" style="width=70" onClick="validateForm(document.buyer_form)">
</td>
</tr>
<tr>
<td colspan="3" align="center"><hr width="760" size="2" color="black" align="center"></td>
</tr>
</table>
</center>

</cfform>


</body>
</html>

I need to add a email validation so I found the following script off the internet. My problem is I cannot seem to incorporate it into my existing code. I think I made all of the necessary changes but when I test by putting in an invalid email, no error message popsup.

I seems like if I make the email to work, the other validations do not work, and vice versa.

How do I incorporate the email validation into my exisintg java validation so that all is validated ?

email validationscript :

function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}
// End -->
</script>

</HEAD>

<BODY>


<form onSubmit="return checkEmail(this)">
E-mail Address:<br>
<input type="text" name="emailAddr">
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>

Thanks for any help.
    This topic has been closed for replies.

    1 reply

    Inspiring
    July 7, 2008
    Things you might want to consider:
    1. You don't need this code;
    <form onSubmit="return checkEmail(this)">
    E-mail Address:<br>
    <input type="text" name="emailAddr">
    <p>
    <input type="submit" value="Submit">
    <input type="reset" value="Reset">
    </form>

    You only need the function checkEmail.


    2. Call your function checkEmail inside your function validateForm.

    returnStatus = checkEmail(buyer_form);

    3. I assume your email field is named supplier_email, so in function checkEmail, change the part
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value))
    to
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.supplier_email.value))


    By the way, I don't quite get your "returnStatus". You declared it as a boolean variable and somehow, somewhere in your code you are calling it like a function?: returnStatus(false);

    Shouldn't it be returnStatus=false? Anyway, maybe I'm wrong. But maybe you need to check on it also.