-2

i tried to look for an answer, but there are too many versions of codes out there, its confusing a noob like me.... i need the phone field to only accept 8 digits, otherwise the form will not submit...

here is the code i got (i added min and mac length but it didnt work:

public function validate( $data )
{
$validator = new AB_Validator();
foreach ( $data as $field_name => $field_value ) {
switch ( $field_name ) {
case 'email':
$validator->validateEmail( $field_name, $data );
break;
case 'phone':
$validator->validatePhone( $field_name, $field_value, $min_length="8", $max_length="8", true );
break;
case 'date_from':
case 'time_from':
case 'time_to':
case 'appointment_datetime':
$validator->validateDateTime( $field_name, $field_value, true );
break;
case 'name':
$validator->validateString( $field_name, $field_value, 255, true, true, 3 );
break;
case 'service_id':
$validator->validateNumber( $field_name, $field_value );
break;
case 'custom_fields':
$validator->validateCustomFields( $field_value );
break;
default:
}
}

if ( isset( $data['time_from'] ) && isset( $data['time_to'] ) ) {
$validator->validateTimeGt( 'time_from', $data['time_from'], $data['time_to']             );
}

return $validator->getErrors();
}

i also tried adding the following script but it only gave me a warning and i'm still able to click submit...

<script type='text/javascript'>

function formValidator(){
// Make quick references to our fields
var phone = document.getElementById('phoneno');

// Check each input in the order that it appears in the form!
        if(isNumeric(phone, "Please enter a valid phone number")){
            if(lengthRestriction(phone, 8, 8)){
                return true;

            }
        }

return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
    alert(helperMsg);
    elem.focus(); // set the focus to this input
    return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
    return true;
}else{
    alert(helperMsg);
    elem.focus();
    return false;
}
}



function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
    return true;
}else{
    alert("Please enter " +max+ " digits");
    elem.focus();
    return false;
}
}

</script>

<script>
$(document).ready(function(){
$("button.next").click(function(){
    $formValidator();
});
});
</script>

Any help would be greatly appreciated...

Bento Set
  • 13
  • 6

2 Answers2

0

"i also tried adding the following script but it only gave me a warning and i'm still able to click submit..."

If the code works but you are still able to click submit (or more importantly, that the form is submittable) after getting a warning that says the criteria for submission hasn't been met, the problem is likely in your <form> element. If you have your onSubmit event handler (presuming you do) in your form calling validation code for the tel. no. field, you can code the function that onSubmit calls to return false or true. If false, the submission is cancelled. If true, it is executed.

If this is all news to you, look at this SO question for guidance:

How to prevent form from being submitted?

Between that and some Googling, you should find out what you need to, assuming this is your problem.

Community
  • 1
  • 1
Matt Campbell
  • 1,967
  • 1
  • 22
  • 34
0

While googling and testing out the form, i noticed the name field had a minimum 3 characters requirement, so i checked the code and noticed this:

case 'name':
$validator->validateString( $field_name, $field_value, 255, true, true, 3 );

so i tweaked and added it to the phone portion, the form is now working as expected, only accepting 8 digits. here is the code added:

case 'phone':
$validator->validatePhone( $field_name, $field_value, true );
$validator->validateString( $field_name, $field_value, 8, true, true, 8 );
Bento Set
  • 13
  • 6