Welcome Guest, Not a member yet? Register   Sign In
acceptable when using javascript?
#1

[eluser]dottedquad[/eluser]
Hello all,
When is it acceptable to use javascript and when it is not acceptable? For example I created a form and perform the first set of validation on client side. When client-side validates javascript then sends the form values via AJAX/JSON to the server-side, but I haven't gotten that for yet. I'm hoping to fulfill this need using Codeigniter.

-Rich
#2

[eluser]nelson.wells[/eluser]
Why would that not be acceptable? Javascript validation on the client side can be used to 1) reduce multiple requests to the server and 2) enhance usability. As long as it degrades gracefully and you have server side validation as well, there is really no reason not to use javascript in a case like this.
#3

[eluser]dottedquad[/eluser]
[quote author="nelson.wells" date="1276050084"]Why would that not be acceptable? Javascript validation on the client side can be used to 1) reduce multiple requests to the server and 2) enhance usability. As long as it degrades gracefully and you have server side validation as well, there is really no reason not to use javascript in a case like this.[/quote]

I thought the same as well, but coding double the amount of work also crossed my mind. I didn't want to waist my time.

-Rich
#4

[eluser]Flemming[/eluser]
double coding - unfortunately it's the way!

After trying lots of different approaches, I've eventually settled on this (using jquery validate):

immediately after my CI validation rules, I write the same jquery validate rules like this...

Code:
$this->form_validation->set_rules('headline', 'Headline', 'trim|required|max_length[250]|xss_clean');
$this->form_validation->set_rules('article_body', 'Article', 'trim|required|xss_clean');

$rules_array = array();
$rules_array[] = 'headline: {required: true, maxlength: 250}';
$rules_array[] = 'article_body: {rte_required: true}';

$messages_array = array();
$messages_array[] = 'headline: {required: "Please enter a headline", maxlength:"The headline can not exceed 250 characters"}';
$messages_array[] = 'article_body: {rte_required: "Please enter the article text"}';

// pass the jquery validation stuff to the view in $data
$data['jqvalidate']['form_add_news']['rules'] = implode(',',$rules_array);
$data['jqvalidate']['form_add_news']['messages'] = implode(',', $messages_array);

then in my view, indside the <head> I check to see if there's any jquery validation to write:

Code:
<?php
if(isset($jqvalidate)) {
    foreach($jqvalidate as $k=>$v) {
?>
    $('#<?php echo $k?>').validate({
        rules: {<?php echo $v['rules']?>},
        messages: {<?php echo $v['messages']?>}
    });
<?php
    }
}
?>

this needs the jquery and the validation plugin but I find it cuts down a fair bit of work and repetition.

If anyone has any slicker way I'd love to hear them!




Theme © iAndrew 2016 - Forum software by © MyBB