简体   繁体   中英

html5 form validation in codeigniter view file

This is my simple html form code

<form method="post" action=<?php echo base_url()."index.php/controller_bookplot2";?> 
 name="frmbooking" id="frmbooking">
  <div class="marginbtm5">
      <select name="name_title" class="dropdown" id="name_title" required>
        <option value="Mr.">Mr.</option>
        <option value="Miss">Miss</option>
        <option value="Mrs">Mrs</option>
        <option value="Dr.">Dr.</option>
      </select>

      <img src=<?php echo base_url()."images/rightmarked.gif";?> title="mandetory"> 
  </div>
  <div class="marginbtm5">
    <input name="name" type="text" class="input validate_B" id="name" onfocus="if (this.value
      == 'Full Name') {this.value=''}" onblur="if(this.value == '') {
      this.value='Full Name'}" value="Full Name" required>

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory">
  </div>
  <div class="marginbtm5">
    <input name="address" type="text" class="input validate_B" id="add" onfocus="if
      (this.value == 'Address') {this.value=''}" onblur="if(this.value ==
      '') { this.value='Address'}" value="Address" required>

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory">
  </div>
  <div class="marginbtm5">
    <input name="email" type="text" class="input validate_B" id="email" onfocus="if
     (this.value == 'Email') {this.value=''}" onblur="if(this.value ==
     '') { this.value='Email'}" required  value="Email" >

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory"
     style="display:none;">
  </div>
  <div class="marginbtm5">
    <input name="dob" type="text" class="input" value="Birth Date" required id="datepicker" >

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory"> 
  </div>
 <div class="marginbtm5">
    <input name="mobno" type="text" class="input validate_B" maxlength="45" onfocus="if
   (this.value == 'Mobile (10 digit mobile no. eg 9812345678)')
    {this.value=''}" onblur="if(this.value == '') { this.value='Mobile
   (10 digit mobile no. eg 9812345678)'}" value="Mobile (10 digit mobile no. eg
    9812345678)" id="mob_no" pattern="[789][0-9]{9}" required     
        onkeyup="javascript:ValidateText(this)" >
  </div>      
    <input name="btnSubmit" type="image" src=<?php echo base_url()."images/submit.jpg";?> value="Submit" onclick="return validate();">
</form>

There are many other fields in my form. But I just put here 5 fields. When I click on submit, It directly displays html5 required message for mobile no field. It dont validate name,address,email,birthdate field first. So What Am I doing wrong??

its not working because of you are giving the value attribute remove value attribute and try like this

<form method="post" action=<?php echo base_url()."index.php/controller_bookplot2";?> 
 name="frmbooking" id="frmbooking">
  <div class="marginbtm5">
      <select name="name_title" class="dropdown" id="name_title" required>
        <option value="">--Please select type</option>
        <option value="Mr.">Mr.</option>
        <option value="Miss">Miss</option>
        <option value="Mrs">Mrs</option>
        <option value="Dr.">Dr.</option>
      </select>

      <img src=<?php echo base_url()."images/rightmarked.gif";?> title="mandetory"> 
  </div>
  <div class="marginbtm5">
    <input name="name" type="text" class="input validate_B" id="name" onfocus="if (this.value
      == 'Full Name') {this.value=''}" onblur="if(this.value == '') {
      this.value='Full Name'}" Placeholder="Full Name" required>

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory">
  </div>
  <div class="marginbtm5">
    <input name="address" type="text" class="input validate_B" id="add" onfocus="if
      (this.value == 'Address') {this.value=''}" onblur="if(this.value ==
      '') { this.value='Address'}" Placeholder="Address" required>

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory">
  </div>
  <div class="marginbtm5">
    <input name="email" type="text" class="input validate_B" id="email" onfocus="if
     (this.value == 'Email') {this.value=''}" onblur="if(this.value ==
     '') { this.value='Email'}" required  Placeholder="Email" >

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory"
     style="display:none;">
  </div>
  <div class="marginbtm5">
    <input name="dob" type="text" class="input" Placeholder="Birth Date" required id="datepicker" >

    <img src=<?php echo base_url()."images/rightmarked.gif";?> title=" mandetory"> 
  </div>
 <div class="marginbtm5">
    <input name="mobno" type="text" class="input validate_B" maxlength="45" onfocus="if
   (this.value == 'Mobile (10 digit mobile no. eg 9812345678)')
    {this.value=''}" onblur="if(this.value == '') { this.value='Mobile
   (10 digit mobile no. eg 9812345678)'}" value="Mobile (10 digit mobile no. eg
    9812345678)" id="mob_no" pattern="[789][0-9]{9}" required     
        onkeyup="javascript:ValidateText(this)" >
  </div>      
    <input name="btnSubmit" type="image" src=<?php echo base_url()."images/submit.jpg";?> value="Submit" onclick="return validate();">
</form>

if you add value to input tag then it will not validate that filed because this filed having already value

No need to wirte onfocus="if (this.value == 'Full Name') {this.value=''}" onblur="if(this.value == '') { this.value='Full Name'}" value="Full Name"

this can be achieve by html attribute placeholder='Full Name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM