Solved

how do you make an email field required into my script.

Posted on 2013-10-28
6
345 Views
Last Modified: 2013-12-18
I have a script which is working fine for question 1.  I would like to make the 'email' field mandatory.  How can you add this to my script.? Some time that field contains automated data from the database, sometime user has to enter his email.  So in case the email field is empty, I would like to ask user 'please add your email'.  how can you add this to my existant javascript.


Here is my code:
E-Mail: <cfoutput><input type="text" name="email" value="#DisplayEmail.email#" size="50"></cfoutput>

<p><label for="strEmail"><strong><strong>1.</strong> If your skill set? </label>
<cfoutput><input type="radio" name="contactme" id="contactme" value="Yes">Yes &nbsp;
<input type="radio" name="contactme" id="contactme" value="No">No</cfoutput>

Here is my JavaScript:


<script type="text/javascript">
// checks buttons (radio, checkbox) - coursesweb.net
function checkButons(frm) {
var re = false;           // used to determine when a button is checked
var ree = false;
var err = '';             // to store the errors

//var question1 = document.getElementById('contactme').checked;          // contains an array with all radio buttons "vegetable"
//alert(question1);
// create an Array in JSonn format with checkbox buttons
//var question2 = [frm.CMTEE_PREF_NEW];
// traverse the radio buttons
// if one is checked sets re to true, and stops the iteration with "break"
for(var i=0; i<frm.contactme.length; i++) {
  if(frm.contactme[i].checked) {
    re = true;
    break;
  }
}
// if "re" is false means no radio button checked, add error in "err" 
if(re == false) err += '- Question 1: Please select « yes » or « no ».';

for(var i=0; i<frm.CIAPermissionProfile.length; i++) {
  if(frm.CIAPermissionProfile[i].checked) {
    ree = true;
    break;
  }
}


// make "re" again False, and traverse the checkbox buttons
// if one is checked sets re to true, and stops the iteration with "break"
re = false
for(var i=0; i<frm.CMTEE_PREF_NEW.length; i++) {
  if(frm.CMTEE_PREF_NEW[i].checked) {
    re = true;
    break;
  }
}

// if "re" is false means no checkbox button checked, add error in "err" 
if (re == false) err += '\n- Question 2: You must check at least one types of committee.';

// if "re" is false means no radio button checked, add error in "err" 
if(ree == false) err += '\n- Question 11: Please select « yes » or « no ». Please try again.';

// if "err" not empty, alert the error(s) and returns False to stop submitting form
if(err != "") {
  alert(err);
  return false;
}
else return re;
}




--></script>

Open in new window

0
Comment
Question by:koila
  • 2
  • 2
6 Comments
 
LVL 32

Accepted Solution

by:
Big Monty earned 250 total points
ID: 39606284
just add the following to your main validation script:

    if( document.getElementById('email').value == '' )
         alert( 'Please fill out your email.' );

Open in new window

0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39674874
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 39674875
I believe the code I provided does exactly what the OP is asking, it should be up to the OP to confirm that it did or did not work.
0
 
LVL 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 250 total points
ID: 39708151
@The_Big_Daddy,

I checked you're code before closing.  There is no ID "email" in the OP's code and hence your code wouldn't work:

E-Mail: <cfoutput><input type="text" name="email" value="#DisplayEmail.email#" size="50"></cfoutput>

The correct output would be to state that the ID needs to be added or another way of obtaining the element be shown.

Given what I've said, feel free to add anything further and I'll review.

tagit
Topic Advisor
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now