File tree Expand file tree Collapse file tree 1 file changed +41
-2
lines changed
Expand file tree Collapse file tree 1 file changed +41
-2
lines changed Original file line number Diff line number Diff line change 3434 label =" SIN"
3535 dense
3636 outlined
37- hide-details
37+ hide-details = " auto "
3838 background-color =" white"
3939 v-model =" student.sin"
40+ :rules =" [validSIN]"
4041 oninput ="
4142 if (this.value.length > 9) this.value = this.value.slice(0, 9);
4243 "
@@ -119,13 +120,19 @@ export default {
119120 },
120121 },
121122 async created () {
122- this .validate = { ... validator };
123+ this .validate = { ... validator, validateSIN : this . validateSIN };
123124 },
124125 methods: {
125126 show () {
126127 this .showDrawer = true ;
127128 },
128129 saveClick () {
130+ const isValidSIN = this .validateSIN (this .student .sin );
131+ if (! isValidSIN) {
132+ alert (" Invalid SIN format" );
133+ return ;
134+ }
135+
129136 axios .post (` ${ STUDENT_URL } ` , this .student ).then ((resp ) => {
130137 console .log (resp .data .data .id );
131138
@@ -134,6 +141,38 @@ export default {
134141 }
135142 });
136143 },
144+
145+ validSIN (input ) {
146+ const isValid = this .validateSIN (input);
147+ return isValid || " Invalid SIN format" ;
148+ },
149+
150+ validateSIN (input ) {
151+ // Remove any non-digit characters
152+ let sin = input .replace (/ \D / g , " " );
153+
154+ // Check if the SIN is exactly 9 digits
155+ if (sin .length !== 9 ) {
156+ return false ;
157+ }
158+
159+ // Validate using the Luhn algorithm
160+ let sum = 0 ;
161+ for (let i = 0 ; i < sin .length ; i++ ) {
162+ let digit = parseInt (sin .charAt (i), 10 );
163+
164+ if (i % 2 === 1 ) {
165+ digit *= 2 ;
166+ if (digit > 9 ) {
167+ digit -= 9 ;
168+ }
169+ }
170+
171+ sum += digit;
172+ }
173+
174+ return sum % 10 === 0 ;
175+ },
137176 },
138177};
139178 </script >
You can’t perform that action at this time.
0 commit comments