Ir al contenido
Menú
Se marcó esta pregunta
10 Respuestas
6928 Vistas

Hello all,

When a new user create an account on our site, what is the easier way to be sure that the zip code is alway entered like :

G8E 5M7 (with capitals letters an a space)

and not like :

g8e5m7

g8e 5m7

G8e 5M7

etc.

Do you have an idea of an easy method to manage it? Or should i dive in python once again?

Avatar
Descartar

In my case (custom module) I did it that way:

	post = record.buyer_postcode.strip(' ')
	code = "%s %s" %(post[:-3].strip(), post[-3:])
	postcode = code.upper()
	record.write({'buyer_postcode': postcode.upper()})
So if you want you can modify py, add action "Validate" or something ....

But in your case it will be better use jQuery as Axel suggested.

Autor

Thanks a lot!

post = record.buyer_postcode.strip(' ')# striping postcode to get only string of characters and digits
	code = "%s %s" %(post[:-3].strip(), post[-3:])#joining it back with one space between
	postcode = code.upper()# changing to uppercase
	record.write({'buyer_postcode': postcode.upper()})# writing new value into the field or database
Mejor respuesta

You could do it using a mask on the input field and normally in combination with a regular expression to validate the format in js and in python to avoid mistakes, you will be ok

See examples here:

https://igorescobar.github.io/jQuery-Mask-Plugin/

Avatar
Descartar
Autor

Thanks Axel. Would you know in which file I should work in odoo 8 to put this code?

Autor

I would say that I have to install myself the jQuery Mask Plugin. Is it the case?

I would try in base/static/src/js/apps.js because in base/res is res_partner so this is the best place to start or as separate module .... but i'm not an expert :) (i'm a total amateur)

Autor

thanks again. I'll see all of this.

Autor Mejor respuesta

Here is our complete solution. Thanks to Dr Obx and Axel.

1) Download jquery mask plugin

2) Install it in ourmodule/static/src/js

3) Load the script

        <template id="ourmodule.assets_frontend" inherit_id="website.assets_frontend" name="Shop">
            <xpath expr="." position="inside">
                <script type="text/javascript" src="/website_lapagept/static/src/js/website_sale_clear_cart.js"></script>
                <script type="text/javascript" src="/website_lapagept/static/src/js/jquery-mask-plugin-master/src/jquery.mask.js"></script>
            </xpath>
        </template>


4) Add this in the file ourmodule/static/src/js/jquery-mask-plugin-master/src/jquery.mask.js

$(document).ready(function(){
//  $('.zipcode').mask('A0A 0A0');
        $('.zipcode').mask('Y9Y 9Y9',
                           {'translation': {
                                        9: {pattern: /[0-9]/},  
                                        Y: {pattern: /[A-Z]/}
                                      }
                                });
});


5) Add the correct .zipcode class to the field that should have the mask

                  <div t-attf-class="form-group #{error.get('zip') and 'has-error' or ''} col-lg-6">
                      <label class="control-label" for="zip">Code postal</label>
                      <input type="text" name="zip" class="form-control zipcode" t-att-value="checkout.get('zip')"/>
                  </div>


AND ALSO

                  <div t-attf-class="form-group #{error.get('shipping_zip') and 'has-error' or ''} col-lg-6">
                      <label class="control-label" for="shipping_zip" style="font-weight: normal">Zip / Postal Code</label>
                      <input type="text" name="shipping_zip" class="form-control zipcode" t-att-value="checkout.get('shipping_zip', '')" t-att-readonly=" 'readonly' if shipping_id &gt;= 0 else ''"/>
                  </div>



And it works fine!

Avatar
Descartar

Exactly as you do it

Publicaciones relacionadas Respuestas Vistas Actividad
1
mar 15
4394
0
sept 23
1871
0
ago 16
4060
0
mar 15
4725
0
jul 24
3437