How to leave current date in text field

Asked

Viewed 18,872 times

2

I would like to leave populated my field text with the data atual, where the mask I have, when the user type leaves configured like this: xx/xx/xxxx. How to do in JS or PHP ?

2 answers

3

You can do it in two ways, take the current date of your server, with PHP, ASP or Java, or take the current date of the client, via JS

From the server, with PHP:

<input id='data' type='text' value='<?php echo date("d/m/Y"); ?>'>

From the client, with JS:

function pegarDataAtual(){
   data = new Date();
   document.getElementById('data').value = data.getDay()+'/'+data.getMonth()+'/'+data.getFullYear();
}

There inside the onload body you can call this function. Simple like this :)

I recommend server-side use, it is better to trust the date that is on your server than the date that comes from the client

If you want to just leave it on the mask, instead of putting it on the value of input, you can put in the attribute placeholder

1

This question can be resolved in this link. If it is in Jquery clear, because you did not specify in which language.

Specify date format Jquery

Example:

$('#yourInputBoxHere').mask("99/99/9999", {placeholder: 'MM/DD/YYYY' });

Or on Html5?

Format Mask Date Html5

Example:

<input type=date step=7 min=2014-09-08>
<input type=time min=9:00 max=17:00 step=900>
<input type=week step=2 min=2014-W30>
  • 1

    Hi Fernando. Could you include examples of the links cited? Link-dependent responses look better as comments (which you need more points to be able to do), as the link can go off the air and the response becomes meaningless. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.