Home > front end >  How can I check if the type DATE is filled?
How can I check if the type DATE is filled?

Time:04-30

I have some questions.

I need to do an appointment form, where I have a bunch of text fields, for the name, email, a DATE field and two dropdown menus. At the end of the form, I have a submit button, but I want it to work only if all of the fields above are filled and if the fields are not filled i have an alert message. I did the verification for the name and email to see if the textboxes are empty or not, but I can't do it for the dropdown menus and the date. The date should be picked by the customer and if it's not picked from the menu I have the placeholder "DD.MM/YYYY". For the dropdown menus I also have placeholders, and I wanna check if the customer has picked one of the options, how can I check if the value of it is the placeholder name that i set? This is the code to check if the name and email fields are filled and it works. But I can't find any code to work to check if date is empty or the dropdown menus option is the same placeholder.

<script language="JavaScript1.1" type="text/javascript">
function popupok() {
ok = false;
test1=false;
const textbox1 = document.getElementById("name");
const textbox2 = document.getElementById("email");

if(textbox1.value.length && textbox2.value.length > 0)
{
    ok=true;
}

if(ok==true)
{
    alert("appointment has been scheduled!")
    
}
else
{
    alert("please fill the required fields!")
}
  
}





</script>



</head>
<body>
 <div >
            <div >
                <div >
                    <div >
                        <div >
                            <h2>Programeaza-te acum!</h2>
                        </div>
                        <form required>
                            <!-- Form start -->
                            <div >
                                <div >
                                    <div >
                                        <label  for="name">Nume</label>
                                        <input id="name" name="name" type="text" placeholder="Nume complet" required>
                                    </div>
                                </div>
                                <!-- Text input-->
                                <div >
                                    <div >
                                        <label  for="email">Email</label>
                                        <input id="email" name="email" type="text" placeholder="E-Mail"  required>
                                    </div>
                                </div>
                                <!-- Text input-->
                                <div >
                                    <div >
                                        <label  for="date">Data programarii</label>
                                        <br/>
                                       <input type="date" id="date" name="date" required>

                                    </div>
                                </div>
                                <!-- Select Basic -->
                                <div >
                                    <div >
                                        <label  for="time">Ora programarii</label>
                                        <select id="time" name="time"  required>
                                            <option value="8:00">8:00</option>
                                            <option value="9:00">9:00</option>
                                            <option value="10:00">10:00</option>
                                            <option value="11:00">11:00</option>
                                            <option value="12:00">12:00</option>
                                            <option value="13:00">13:00</option>
                                            <option value="14:00">14:00</option>
                                            <option value="15:00">15:00</option>
                                            <option value="16:00">16:00</option>
                                            <option value="17:00">17:00</option>
                                            <option value="18:00">18:00</option>
                                            <option value="19:00">19:00</option></option>
                                            
                                        </select>
                                    </div>
                                </div>
                                <!-- Select Basic -->
                                <div >
                                    <div >
                                        <label  for="appointmentfor">Serviciul dorit</label>
                                        <select id="appointmentfor" name="appointmentfor"  required>
                                            <option value="Service#1">Coafor</option>
                                            <option value="Service#2">Cosmetica</option>
                                            <option value="Service#3">Manichiura</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- Button -->
                                
                                <div >
                                <br/>
                                    <div >
                                        <button id="singlebutton" name="singlebutton" type="submit"  onclick="popupok()">Rezerva locul!</button>
                                    </div>
                                </div>
                            </div>
                        </form>```

CodePudding user response:

You can -- the date seems to be an empty string, so you can simply do:

if (date) {
  // date isn't an empty string, null, or undefined.
}

const input = document.getElementById("input");

console.log(`${input.value} (has value: ${!!input.value})`)

input.addEventListener("change", () => {
  console.log(`${input.value} (has value: ${!!input.value})`)
})
<input type="date" id="input">

CodePudding user response:

use:

 const DROPDOWN = document.querySelector('.dropdown');
    if(DROPDOWN.value === '') { 
// do something  if empty 
} 
    if(DROPDOWN.value === 'option value') {
 // do something 
}

//every option has a value. in html you should specify it in tag by value = 'something';

to check if dropdown is chosen or not. although I don't know what kind of dropdown it is.

for date just do the same thing but like

 if(DATE.innerHTML === "DD.MM/YYYY") { //your placeholder
   // do something
} 

or

 if(DATE.value === "DD.MM/YYYY") { //your placeholder
   // do something
} 

if the user hasn't changed the date it would be same as your placeholder.

  • Related