2011-09-16 4 views
1

시간을 입력 할 수있는 텍스트 상자가 있고 jquery를 사용하여 유효성을 검사하는 데 사용합니다. 하지만 이제는 codeigniter의 내장 검증 시스템을 사용하여 텍스트 상자의 유효성을 검사하고 싶습니다. codeigniter에 내장 된 검증 시스템을 사용하여 시간 입력을 검증하는 방법을 알려주시겠습니까?Codeigniter에서 시간 입력을 검증하는 방법

는 여기에 jQuery를 사용하여 작업을 수행하는 데 사용하는 방법 코드입니다 :

<script type="text/javascript"> 

$().ready(function() { 

$.validator.addMethod("time", function(value, element) { 
return this.optional(element) || /^(([0-1]?[0-2])|([2][0-3])):([0-5]?[0-9])\s(a|p)m?$/i.test(value); 
}, "Enter Valid Time"); 

    $("#theForm").validate({ 
      rules: { 
        time: "required time", 
      }, 

    }); 

}); 

</script> 

을 그리고 여기이 아마

// Validation rule in controller 
$this->form_validation->set_rules('time', 'time', 'trim|min_length[3]|max_length[5]|callback_validate_time'); 

콜백 같은

<input class="time" type="text" name="time1" size="15"> 

답변

5

뭔가 html로에게 있습니다 :

public function validate_time($str) 
{ 
//Assume $str SHOULD be entered as HH:MM 

list($hh, $mm) = split('[:]', $str); 

if (!is_numeric($hh) || !is_numeric($mm)) 
{ 
    $this->form_validation->set_message('validate_time', 'Not numeric'); 
    return FALSE; 
} 
else if ((int) $hh > 24 || (int) $mm > 59) 
{ 
    $this->form_validation->set_message('validate_time', 'Invalid time'); 
    return FALSE; 
} 
else if (mktime((int) $hh, (int) $mm) === FALSE) 
{ 
    $this->form_validation->set_message('validate_time', 'Invalid time'); 
    return FALSE; 
} 

return TRUE; 
} 
MM :
0

HH 같은 시간을 수용하기 위해, 게시 @danneth 무엇을 수정하는 경우

* 추가 ​​된 또 하나의 조건을 폭발에 대한

public function validate_time($str){ 
    if (strrchr($str,":")) { 
     list($hh, $mm, $ss) = explode(':', $str); 
     if (!is_numeric($hh) || !is_numeric($mm) || !is_numeric($ss)){ 
      return FALSE; 
     }elseif ((int) $hh > 24 || (int) $mm > 59 || (int) $ss > 59){ 
      return FALSE; 
     }elseif (mktime((int) $hh, (int) $mm, (int) $ss) === FALSE){ 
      return FALSE; 
     } 
     return TRUE; 
    }else{ 
     return FALSE; 
    } 
} 

*가되지 않는 분할을 변경 SS

을받은 매개 변수는 문자열입니다 'AAAAA'

* 변경 양식 유효성 검사의 MIN_LENGTH에서 3 ~ 8처럼 (SS 입력 그래서 당신은 HH에서 8 자 확인하십시오 : MM)을 :

$this->form_validation->set_rules('time','Time','required|trim|min_length[8]|max_length[8]|callback_validate_time'); 
0

은 즉석 @danneth의 코드가 도움이되기를 바랍니다.

function _validate_date($str_date) 
    { 
     if($str_date!='') 
     { 
      /* 
       Remove Whitespaces if any 
      */ 
      $str_date = trim($str_date); 

      list($hh,$sub) = split('[:]', $str_date); 

      /* 
       Separate Minute from Meridian 
       e.g 50 PM ===> $mm=60 and $md=PM 
      */ 
      $mm = substr($sub, 0,2); 
      $md = trim(substr($sub, 2,strlen($sub))); 

      /* 
       Make Meridian uppercase 
       Implicitly Check if Meridian is PM or AM 
       if not then make it PM 
      */ 

      $md = strtoupper($md); 

      if(!in_array($md, array('PM',"AM"))) 
      { 
       return FALSE; 
      } 

      /* 
       Check if MM and HH are numeric 
      */ 
      if(!is_numeric($hh) || !is_numeric($mm)) 
      { 
       $this->form_validation->set_message('Invalid Time','Illegal chars found'); 
       return 11; 
      } 

      $hh = (int)$hh; 
      $mm = (int)$mm; 

      /* 

       Check HH validity should not be less than 0 and more 24 
      */ 

      if($hh<0 || $hh>24) 
      { 
       return FALSE; 
      } 


      /* 

       Check MM validity should not be less than 0 and more than 59 
      */ 

      if($mm<0 | $mm>59) 
      { 
       return FALSE; 
      } 

      /* 
       Parse HH and MM to int for further operation and check it generates validate time 
      */ 

      if(mktime($hh,$mm)==FALSE) 
      { 
       $this->form_validation->set_message('Invalid Time','Check Time'); 
       return FALSE; 
      } 


      return TRUE;  


     } 
     else 
     { 
      return FALSE; 
     } 
    } 
관련 문제