Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Required Fields 1

Status
Not open for further replies.

powerlock2005

Technical User
Feb 23, 2005
42
0
0
US
All,
I have the following code for required fields and it works fine:

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

<!-- hide from older browsers -->

function validForm(Form) {
if (Form.arrivalDate.value =="")
{
alert("Please enter your arrival date.")
Form.arrivalDate.focus()
return false
}
if (Form.departureDate.value =="")
{
alert("Please enter your departure date.")
Form.departureDate.focus()
return false
}
return true
}

// end hiding script -->
//-->
</script>

I need to add some if then statements for the above and I don't know how to do it using javascripts. I know it for visual basic, but not javascripts.

This is what I need to do:

I have a field called: hotel, which is a value list of Yes or No. I have 2 date fields, which are in the script above called: arrivalDate and departureDate.

If the value of hotel is Yes, then I want arrivalDate and departureDate to be required, but if the value of hotel is No, then arrivalDate and departureDate doesn't have to be required.

How do I do this?

Please help and thanks in advance.
 

Try this:

Code:
function validForm(Form) {
	if (Form.elements['yourSelectBoxName'].selectedIndex == 0) {
		if (Form.arrivalDate.value == '') {
			alert('Please enter your arrival date.')
			Form.arrivalDate.focus()
			return false
		}
		if (Form.departureDate.value == '') {
			alert('Please enter your departure date.')
			Form.departureDate.focus()
			return(false);
		}
	}
	return(true);
}

You'll have to plug in the name of your select box element, and ensure that the first item is "Yes".

It was really only the first line I've added.

Hope this helps,
Dan

 
Dan,
You know your STUFF!!!!

That work beautifully!!!!

Thanks so much for helping me again, and you are also teaching me javascripts. I use visual basic a lot, and it seems this is somewhat similar, so I'm going to have to get some books.

Can you recommend me some good books on javascripts, that are good for amateurs like me?

Thanks again for your help!!!!
 
Dan,
I have another one for you, if you can help me.

I have a field called: dateEntered.

I would like for that field to automatically have today's date.

How do I do this?

Your help is greatly appreciated.

Thanks,
 

Personally, I would do this on page load. If your form is called 'myForm', this would work, assuming you wanted DD-MM-YYYY format:

Code:
function setDate() {
	var now = new Date();
	var dateText = now.getDate() + '-' + (parseInt(now.getMonth(), 10) + 1) + '-' + now.getFullYear();
	document.forms['myForm'].elements['dateEntered'].value = dateText;
}

...

<body onload="setDate();">

Obviously, you might want to add your own routines to pad out single digits into double digits, etc... or swap the order of the DD and MM around.

Hope this helps,
Dan

 
Dan,
I changed the order around to meet this requirement: yyyy/mm/dd, but I need double digits for the mm and dd, and I don't know how to get double digits.

Maybe I can search around the internet to figure that out, but Man I really appreciate you assisting me with this:)
 

Something like this:

Code:
function padDigits(num) {
   if (num < 10) return(num);
   return('0' + num);
}

It can be called within the string concatenation line:

Code:
var dateText = padDigits(now.getDate()) + '-' + padDigits((parseInt(now.getMonth(), 10) + 1)) + '-' + now.getFullYear();

Dan

 
Hey Dan,
I ended up using the below:

<script>

/*Current date in form credit:
JavaScript Kit (Over 200+ free scripts here!
*/

var mydate=new Date()
var theyear=mydate.getYear()
if (theyear < 1000)
theyear+=1900
var theday=mydate.getDay()
var themonth=mydate.getMonth()+1
if (themonth<10)
themonth="0"+themonth
var theday=mydate.getDate()
if (theday<10)
theday="0"+theday

//////EDIT below three variable to customize the format of the date/////

var displaythird=theyear
var displayfirst=themonth
var displaysecond=theday


////////////////////////////////////

document.FrontPage_Form1.dateEntered.value=displaythird+"/"+displayfirst+"/"+displaysecond
</script>

It seems to be working ok:)

Brother I REALLY appreciate all the help you have given me, and I will give you a STAR!!!!

Thanks my friend,
Jerome
 

I can't believe that a company that is giving away scripts would use this:

Code:
var theyear=mydate.getYear()
if (theyear < 1000)
theyear+=1900

instead of this:

Code:
var theyear = mydate.getFullYear();

Shocking!

Dan

 
Maybe that's why the date didn't show up correctly in the MySQL database when the form is submitted.

I changed it based on what you provided.

Thanks for that heads-up!!!!

Have a good day Dan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top