Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Some Javascript help please..form validating

Featured Replies

Hello

 

I have a form with a checkbox which when checked I would like another field to become 'required' i.e. show a message saying they need to fill in field before form is submitted.

 

I've done this code within javascript <script>....

 

function reqHotel() {
	
	 if (document.getElementById("send_info_5").checked == true &&	    
        document.getElementById("other_9").value.length == 0 )	    
    {

        //then show an alert.....
		alert("Please enter the name of the Hotel you wish your Travel Card to be available to collect from.")
        // and also set the cursor to the state field.

	
        return false
    }
	
}

 

Then in the form tag I've added the function on submit... onSubmit=" return reqHotel();"

 

But it's not working...

 

 

Any ideas?

 

Many thanks in advance

 

Vicky

 

Do you want to return true when validation passes, or submit form using javascript?

 

<script>
function reqHotel() {

alert('hello');
return true;

}
</script>

<form onSubmit=" return reqHotel();">
<input type="submit" value="hello"/>
</form>

 

 

works fine

  • Author

Not getting anywhere with this...

 

should the following in theory work?....

 

 

<script type="text/javascript">

// CHANGES REQUIRED FIELDS WHEN TRAVEL CARD SELECTED - MAKE HOTEL FIELD REQUIRED
function reqHotel() {
	
	 if (document.(send_info_5).checked && document.(other_9).value.length == 0)
    {
		

        //then show an alert.....
		alert("Please enter the name of the Hotel you wish your Travel Card to be available to collect from.")
        
		
		return true;
    }
	
}

</script>

</head>

<body>

    <form  onsubmit="return reqHotel();" action="#"  >

 

Thanks

lol it is logic issue

 

here is working code :) with checkbox and field. I assum you want your check box be clicked selected and your text field have some data

 

 

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

// CHANGES REQUIRED FIELDS WHEN TRAVEL CARD SELECTED - MAKE HOTEL FIELD REQUIRED
function reqHotel() {
     if (!document.getElementById('send_info_5').checked || document.getElementById('other_9').value == '') {
        alert("Please enter the name of the Hotel you wish your Travel Card to be available to collect from.")
        return false;
    }
    return true;
    
}

</script>

</head>

<body>


    <form onSubmit=" return reqHotel();">

     <input type="checkbox" name="send_info_5" id="send_info_5" />
     <input type="text" name="other_9" id="other_9" />
     <input type="submit" value="hello"/>
    </form>
    </body>
 

Edited by pulsphoto

  • Author

Thanks everso - you are a star! Logic was definitely lacking from my brain yesterday!

 

It's working to a point. The only issue is that it seems to bring up the alert message regardless of whether the check box is checked or not. I tried changing the || to && but that didn't seem to work. I want the message only to come up if they have checked the box and the alert field is empty. If they haven't checked it and the box is empty I don't need it to come up.

 

Also there is another function on this form too which determines the url it is redirected to dependent on whether the checkbox is checked or not and I can't seem to get both functions to work at same time... (there is also another funciton to show / hide the hotel box depending on whether travel card box is checked just below the checkbox within form)

 

 

<script type="text/javascript">

// CHANGES REQUIRED FIELDS WHEN TRAVEL CARD SELECTED - MAKE HOTEL FIELD REQUIRED
function reqHotel() {
     if (!document.getElementById('send_info_5').checked || document.getElementById('other_9').value == '') {
        alert("Please enter the name of the Hotel you wish your Travel Card to be available to collect from.")
        return false;
    }
    return true;
    
}

</script>



<script type="text/javascript">

//function to switch redirect if travel card is ticked

function switchtravel() {
	
	if (document.getElementById('send_info_5').checked) {
		//get
		url = "http://web_address1.html";
		//set
		$('#redirect').val(url);
		
		return false;
	}
	else {
		//get
		url = "http://web_address2.html";
		//set
		$('#redirect').val(url);
	
		return false;
	}

}

</script>


<form onSubmit="return reqHotel(); return ChooseDayPass();"     


futher down in form.....

<td><span class="bodycopyboldlarger"> I would like a complimentary Travel Card</span> <br /><br /> <p>The complimentary T-10 travel card, will entitle you to "a multi-person travel card valid for 10 intermodal journeys from 1 to 6 zones. </p>
<div id="hotel_info">
Your Travel card will be available for collection when you check in at your hotel.<br /><br />
<label for="other_9">Hotel you will be staying at:</label>
<span class="bodycopyRed">*</span> <input name="other_9" type="text" id="other_9" size="28" maxlength="45" onkeypress="return noenter()" /></div>
<td valign="top"><input name="send_info_5" type="checkbox" id="send_info_5" value="I will require a Travel Card" onClick='switchtravel(this)'></td>

<script type="text/javascript">

<!--
if ($("#send_info_5").not(":checked")) {
$("#hotel_info").hide();
}
$(document).ready(function() {
$("#send_info_5").click(function() {
if ($(this).is(":checked")) {
$("#hotel_info:hidden").show();
$("#other_9").trigger("focus");
} else {
$("#hotel_info:visible").hide();
}
});
});
-->
</script>

 

 

 

 

Any ideas much appreciated.

 

Thanks in advance

 

Vicky

Edited by kanger

  • Author

Actually no worries, thanks for help, but I've sorted it all :) :) :) always happens after posting.

 

:)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.