This question has been flagged
10 Replies
5684 Views

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
Discard

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.

Author

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
Best Answer

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
Discard
Author

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

Author

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)

Author

thanks again. I'll see all of this.

Author Best Answer

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
Discard

Exactly as you do it