Postingan lainnya
Codeigniter form validation
saya mau tanya, saya ngikut di tutorial <a href='http://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html'>http://mbahcoding.com/tutorial/php/codeigniter/codeigniter-ajax-crud-modal-server-side-validation.html</a> disitu saya nemu validasi yg agak beda yg sama saya gunakan biasanya, scriptnya kaya dibawah
private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this->input->post('firstName') == '')
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name is required';
$data['status'] = FALSE;
}
if($this->input->post('lastName') == '')
{
$data['inputerror'][] = 'lastName';
$data['error_string'][] = 'Last name is required';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}
kalo saya mau coba implement pake form validation yg biasa bagaimana ya, seperti script dibawah, karena script diataskan cuma ketika fieldnya kosong aja.. saya mau coba implement min_length atau max_length misalkan pake script diatas
$this->form_validation->set_rules('firstName', 'First Name', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('lastName', 'Last Name', 'trim|required|min_length[5]|max_length[12]');
15 Jawaban:
Tinggal ditambahin didalam if () nya gan
Tambahkan bagian ini:
//buat dulu variable min max length nya
$minLength = 5;
$maxLength = 12;
$lengthFirstname = strlen($this->input->post('firstName'));
$lengthLastname = strlen($this->input->post('lastName'));
//Tambahkan ini && $lengthFirstname <= $maxLength && $lengthFirstname >= $minLength
if($this->input->post('firstName') == '' && $lengthFirstname <= $maxLength && $lengthFirstname >= $minLength)
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name is required';
$data['status'] = FALSE;
}
//Tambahkan ini && $lengthLastname <= $maxLength && $lengthLastname >= $minLength
if($this->input->post('lastName') == '' && $lengthLastname <= $maxLength && $lengthLastname >= $minLength)
{
$data['inputerror'][] = 'lastName';
$data['error_string'][] = 'Last name is required';
$data['status'] = FALSE;
}
"kok tetap ga bisa di eksekusi ya gan "
ngga bisa di eksekusi apanya ya gan? sudah bisa masuk validasinya? sudah di cek echo $lengthFirstname dan $lengthLastname nya? apakah sudah masuk ke if nya? (bisa di echo) apakah saat cek $data[status] berhasil exit nya?
Sory gan, ane salah naroh validasinya:
//untuk firstName
if($this->input->post('firstName') == '')
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name is required';
$data['status'] = FALSE;
}
else if ($lengthFirstname > $maxLength)
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name maxLength is ' . $maxLength;
$data['status'] = FALSE;
}
else if ($lengthFirstname < $minLength)
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name minLength is ' . minLength;
$data['status'] = FALSE;
}
//untuk lastName
if($this->input->post('lastName') == '')
{
$data['inputerror'][] = 'lastName';
$data['error_string'][] = 'Last name is required';
$data['status'] = FALSE;
}
else if ($lengthLastname > $maxLength)
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name maxLength is ' . $maxLength;
$data['status'] = FALSE;
}
else if ($lengthLastname < $minLength)
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'First name minLength is ' . minLength;
$data['status'] = FALSE;
}
oke samasama, jgn lupa diresolved yaa gan (pilih jawaban terbaik)
kalau untuk khusus tidak boleh angka gimana gan ??? atau khusus tidak boleh abjad
"kalau untuk khusus tidak boleh angka gimana gan ??? atau khusus tidak boleh abjad"
Anda bisa gunakan 2 cara: 1. menggunakan preg_match (atau sebutannya regex) link: https://stackoverflow.com/questions/4878194/regex-to-validate-only-natural-numbers/4878242 2. menggunakan ctype function (ada banyak pilihan, lihat link) link: http://php.net/manual/en/book.ctype.php
Cara 1: preg_match
preg_match Khusus Abjad:
$firstName = $this->input->post('firstName');
if (preg_match("/([A-Za-z]+)/", $firstName)) {
// contains only A-Z
} else {
// contains other stuff
}
preg_match Khusus Angka:
$firstName = $this->input->post('firstName');
if (preg_match('/^[0-9]+$/', $firstName)) {
// contains only 0-9
} else {
// contains other stuff
}
Cara 2: ctype function
ctype function Khusus Abjad:
$firstName = $this->input->post('firstName');
if (ctype_alpha($firstName)) {
// contains only A-Z
} else {
}
ctype function Khusus Angka:
$firstName = $this->input->post('firstName');
if (ctype_digit($firstName)) {
// contains only 0-9
} else {
// contains other stuff
}
betul gini ??? atau emang salah ???
$firstName= $this->input->post('firstName');
if (preg_match('/^[0-9]+$/', $firstName))
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'KHUSUS ANGKA !!!';
$data['status'] = FALSE;
}
atau
$firstName= $this->input->post('firstName');
if (preg_match('/^[0-9]+$/', $firstName))
{
echo "ada angka";
}else{
echo "tidak ada angka";
}
tapi kalau gini jadi error
Message: syntax error, unexpected 'else' (T_ELSE)
iya ini betul gan
$firstName= $this->input->post('firstName');
if (preg_match('/^[0-9]+$/', $firstName))
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'KHUSUS ANGKA !!!';
$data['status'] = FALSE;
}
kok tidak ke eksekusi ya, apa ada yg salah
coba tambahkan "!" sebelum preg_match
$firstName= $this->input->post('firstName');
if (!preg_match('/^[0-9]+$/', $firstName))
{
$data['inputerror'][] = 'firstName';
$data['error_string'][] = 'KHUSUS ANGKA !!!';
$data['status'] = FALSE;
}
gan tau cara biki dropdown list yang langsung terhubung ke database ?