Jump to content

Hello. I have a form made where the client wants a date input to only allow Sundays. I'd prefer to do this with javascript. Anyone know a way where I can get this to work? Here's what I have so far but no luck. ID for the html input with type='date' is 'endDate'

<script>
function onlySundays() {
    var selectedDate = document.getElementById("endDate").value;
    var dayNumber = selectedDate.getUTCDate();
 
if(dayNumber !== 0) {
    document.getElementById('endDate').value = "";
    }else {
    }
}
</script>
Link to comment
https://linustechtips.com/topic/1051278-help-with-javascript/
Share on other sites

Link to post
Share on other sites

Instead of this:

24 minutes ago, THEREALJUICYJ said:
    var selectedDate = document.getElementById("endDate").value;

Use:

    var selectedDate = document.getElementById("endDate").valueAsDate;

 

Also, this bit returns day of MONTH, not day of week.

24 minutes ago, THEREALJUICYJ said:

var dayNumber = selectedDate.getUTCDate();

Use getUTCDay() instead.

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1051278-help-with-javascript/#findComment-12451109
Share on other sites

Link to post
Share on other sites

17 minutes ago, WereCatf said:

Instead of this:

Use:


    var selectedDate = document.getElementById("endDate").valueAsDate;

 

Also, this bit returns day of MONTH, not day of week.

Use getUTCDay() instead.

 

Even when just this is the script while the input has 'onchange = 'testing()' 

<script>
function testing() {
var selectedDate = document.getElementById("endDate").valueAsDate;
 
 
document.getElementById('testing').innerhtml = selectedDate;
}
</script>

it will not output to a "<p id='testing'></p>" in the page. Even with just a regular .value. I feel like it just cant grab the value in the date input. Or I just don't know how to.

Link to comment
https://linustechtips.com/topic/1051278-help-with-javascript/#findComment-12451163
Share on other sites

Link to post
Share on other sites

9 minutes ago, THEREALJUICYJ said:

Even when just this is the script while the input has 'onchange = 'testing()' 

<script>
function testing() {
var selectedDate = document.getElementById("endDate").valueAsDate;
 
 
document.getElementById('testing').innerhtml = selectedDate;
}
</script>

it will not output to a "<p id='testing'></p>" in the page. Even with just a regular .value. I feel like it just cant grab the value in the date input. Or I just don't know how to.

You cannot just write an object-type of 'date' to a web-page. You need to convert it into text or set it in an element that supports such a type, like e.g. as follows:

document.getElementById("testing").innerHTML = selectedDate.toISOString().substr(0,10);

 

Secondly, learn to use your browser's debug-tools. In e.g. Firefox, the console-log would've told you the error.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1051278-help-with-javascript/#findComment-12451204
Share on other sites

Link to post
Share on other sites

13 minutes ago, WereCatf said:

You cannot just write an object-type of 'date' to a web-page. You need to convert it into text or set it in an element that supports such a type, like e.g. as follows:


document.getElementById("testing").innerHTML = selectedDate.toISOString().substr(0,10);

 

Secondly, learn to use your browser's debug-tools. In e.g. Firefox, the console-log would've told you the error.

Okay will do I've just started learning programming a month ago. Good to know

Link to comment
https://linustechtips.com/topic/1051278-help-with-javascript/#findComment-12451251
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×