Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
18763 มุมมอง

Hi all,

i'm testing some validation functions for the phone field to avoid others characters than numbers from 0 to 9.

    def valide_tel(self, cr, uid, ids, context=None):
        record = self.browse(cr, uid, ids)
        pattern = "^\+?[0-9]*$"
        for data in record:
            if re.match(pattern, data.phone) == None:
                return False
        return True



I tested the regex on pythex.org with success, but the problem is that when no phone number is entered, Odoo complains about ValidationError: expected string or buffer.

What can i do to have this working?

Thanks

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Julius,

When there is no value entered in the phone field, it will take it as False, and regular expression will work on string only.

So, either you make your phone field as mandatory to the user or before implementing 'if condition' check if the value of data.phone is not False .

Hope this will help you!    


 

อวตาร
ละทิ้ง
ผู้เขียน

I added the condition and it works: if (data.phone != False and re.match(pattern, data.phone) == None): Thank you Pawan !

คำตอบที่ดีที่สุด

Hello, 

You will find  \this\\ and\&nbsp\;\this Helpful.


Thanks

อวตาร
ละทิ้ง

http://stackoverflow.com/questions/16135069/python-validation-mobile-number and http://stackoverflow.com/questions/16125964/python-trying-to-validate-input-such-as-a-phone-number-to-ensure-it-is-10-numer i am not able to post link in answer (may due to bug) Thanks

It works when you paste the link twice into the form :-)

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
พ.ค. 24
1677
0
ม.ค. 21
4805
1
พ.ค. 18
4807
1
เม.ย. 24
1910
1
เม.ย. 24
1128