SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Does this code look wise?

    Hi all
    Quick question please, this code is inline in my webpage, it doesn't look like it is the most efficient way. It appears to be checking for required fields on submit. What do you suggest? Thank-you

    Code:
    			<script>;var std_required_fields=["first_name","last_name","email","shipping_address","document_type_id","document_number","marital_status_id","cellphone","vigencia_date","license_plate","manufacturer_id","vehicle_type_id","motor_number","chasis_serial_number","phone","dir_env","hora_entrega"];
    var std_required_fields_labels=[];
    
     std_required_fields_labels["first_name"]="Nombres";
     std_required_fields_labels["last_name"]="Apellidos";
     std_required_fields_labels["email"]="Correo Electronico";
     std_required_fields_labels["shipping_address"]="Direccion";
     std_required_fields_labels["document_type_id"]="Tipo de Documento";
     std_required_fields_labels["document_number"]="Documento";
     std_required_fields_labels["marital_status_id"]="Estado Marital";
     std_required_fields_labels["cellphone"]="Celular";
     std_required_fields_labels["vigencia_date"]="Vigencia";
     std_required_fields_labels["license_plate"]="Placa";
     std_required_fields_labels["manufacturer_id"]="Marca";
     std_required_fields_labels["vehicle_type_id"]="Tipo de vehiculo";
     std_required_fields_labels["motor_number"]="No Motor";
     std_required_fields_labels["chasis_serial_number"]="Serial Chasis";
     std_required_fields_labels["phone"]="Telefono";
     std_required_fields_labels["dir_env"]="Direccion Envio";
     std_required_fields_labels["hora_entrega"]="Hora Entrega";
    </script>
    			<form  onsubmit='return (std_validate_required_fields(validate_form2) && std_form_check()&&validate_form()) ' action='?mod=user&ac=main_form_ok'  method='POST'>

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That code is bad. JavaScript doesn't do associative arrays. What you need is an object literal (a hash table). You should change the std_required_fields_labels array to containing only the Spanish words:

    Code javascript:
    var labels = ['Nombres','Apellidos', ... ];
    var required_fields=["first_name","last_name","email","shipping_address","document_type_id","document_number","marital_status_id","cellphone","vigencia_date","license_plate","manufacturer_id","vehicle_type_id","motor_number","chasis_serial_number","phone","dir_env","hora_entrega"];
     
     
    // Now make the object:
    var fields = {};
    for (var i = 0, j = labels.length; i++) {
      fields[required_fields[i]] = labels[i];
    }
    I'd also change the names of the variables to be less horribly long and similar-looking.

    The only disadvantage with this looping method is that you have to be sure the labels and required_fields arrays are the same length (and the indices match exactly).

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply really appreciated.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •