-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateData.c
1303 lines (1156 loc) · 36.7 KB
/
validateData.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
*
* validateData.c
*
* This program is responsible for validating the information
* that the user entered if it is valid or not.
*
* Created by Jiajia Bai (Jia) ID 63070503404
* Sonia Gautam (Sonia) ID 63070503410
* Tamonwan Boonpa (Nice) ID 63070503418
* Theeranont Ruchiyakorn (Peak) ID 63070503420
*
* 18 April 2021
*
********************************************************************
*/
#include "datingApp.h"
#include "abstractHashTable.h"
/* public function */
/**************************************************************
*
* This function deletes the space after the input and space
* that user may accidentally entered.
*
* input[] is the input data from the user
*
*************************************************************/
void deleteSpace(char input[])
{
int i = strlen(input) - 1; /* i is a length of the variable - 1 */
while ((i >= 0) && (isspace(input[i])))
{
input[i] = '\0';
i--;
}
}
/* public function */
/**************************************************************
*
* This function checks the input if the user wrote nothing
* and pressed enter or not.
*
* input[] is the input data from the user
*
* This function returns 'result' which is 0 when nothing
* is written, otherwise 1.
*
*************************************************************/
int checkEnter(char input[])
{
int result = 0; /* to store return value */
if ((input[0] != '\n') && (input[0] != '\r'))
{
result = 1;
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input contains a space or not.
*
* input[] is the input data from the user
*
* This function returns 'result' which is 0 when space is
* found in the string, otherwise 1.
*
*************************************************************/
int checkSpace(char input[])
{
int i = 0; /* loop variable*/
int result = 1; /* to store return value */
for (i = 0; i < strlen(input); i++)
{
if (isspace(input[i]))
{
result = 0;
break;
}
}
return result;
}
/* public function */
/**************************************************************
*
* This function checks if the name contains only letters,
* and spaces or not
*
* name[] is the input name from the user.
*
* This function returns 'result' which is 1 when the input
* contains only letters and spaces, otherwise 0.
*
*************************************************************/
int validateName(char name[])
{
int i = 0; /* loop variable */
int result = 1; /* to store return value */
for (i = 0; i < strlen(name); i++)
{
if ((isalpha(name[i]) == 0) && (isspace(name[i]) == 0))
{
printf("\t\tNot valid - must be only any combination of letters and spaces\n");
result = 0;
break;
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input follows the rules
* below or not.
*
* Arguments
* input[] - the input data from the user
* checkSpecial - to store status if invalid
* special character is found
* checkDot - to store status if dot is
* found aside '@' sign
* checkAt - to count number of '@' sign
* found in the input
*
*************************************************************/
void checkString(char input[], int* checkSpecial, int* checkDot, int* checkAt)
{
int i = 0; /* loop variable */
for (i = 0; i < strlen(input); i++)
{
if(!isupper(input[i]) && !islower(input[i]) && !isdigit(input[i])&& !(input[i] == '@')
&& !(input[i] == '.') && !(input[i] == '_') && !(input[i] == '-'))
{
*checkSpecial = 0;
}
}
for (i = 1; i < strlen(input); i++)
{
if ((input[i] == '.') && (input[i-1] == '@'))
{
*checkDot = 0;
}
else
{
if ((input[i-1] == '.') && (input[i] == '@'))
{
*checkDot = 0;
}
}
}
for ( i = 0; i < strlen(input); i++)
{
if(input[i] == '@')
{
(*checkAt)++;
}
}
}
/* private function */
/**************************************************************
*
* This function locates the index where the '@' exists.
*
* input[] is the input email from the user.
*
* This function returns 'index' which is the index number
* where the at-sign is.
*
*************************************************************/
int checkSign(char input[])
{
int i = 0; /* loop variable */
int index = 0; /* index number where the '@' is */
for (i = 0; i < strlen(input); i++)
{
if(input[i] == '@')
index = i;
}
return index;
}
/* private function */
/**************************************************************
*
* This function checks if the input contains the valid
* top-level domain or not.
*
* input[] is the input email from the user.
*
* This function returns 'result' which is 1 when the input
* contains valid TLD, otherwise 0.
*
*************************************************************/
int checkTLD(char input[])
{
int i = 0; /* loop variable */
int j = 0; /* loop variable */
int result = 0; /* to store return value */
char checkString[MAXLEN]; /* store the TLD from the input */
memset(checkString,'\0',sizeof(checkString));
for (i = 0; i < strlen(input); i++)
{
if (input[i] == '.')
{
memset(checkString,'\0',sizeof(checkString));
for (j = i; j < strlen(input); j++)
{
checkString[j-i] = input[j];
}
if ((strcmp(checkString,".com") == 0) || (strcmp(checkString,".net") == 0) ||
(strcmp(checkString,".ac.th") == 0) || (strcmp(checkString,".co.th") == 0))
result = 1;
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function locates the position of the TLD.
*
* input[] is the input email from the user.
*
* This function returns 'index' which is the number
* where the TLD is.
*
*************************************************************/
int locateTLD(char input[])
{
int i = 0; /* loop variable */
int j = 0; /* loop variable */
int index = -1; /* index number where the '@' is */
char checkString[MAXLEN]; /* store the TLD from the input */
memset(checkString,'\0',sizeof(checkString));
for (i = 0; i < strlen(input); i++)
{
if(input[i] == '.')
{
memset(checkString,'\0',sizeof(checkString));
for(j = i; j < strlen(input); j++)
{
checkString[j-i] = input[j];
}
if ((strcmp(checkString,".com") == 0) || (strcmp(checkString,".net") == 0) ||
(strcmp(checkString,".ac.th") == 0) || (strcmp(checkString,".co.th") == 0))
index = i;
}
}
return index;
}
/* private function */
/**************************************************************
*
* This function checks if the input email contains
* digits or alphabets before TLD or not.
*
* input[] is the input email from the user.
*
* This function returns 'result' which is 1 when the input
* contains digits or alphabets before TLD, otherwise 0.
*
*************************************************************/
int alphaNumeric(char input[])
{
int i = 0; /* loop variable */
int result = 0; /* to store return value */
for (i = checkSign(input) + 1; i < locateTLD(input); i++)
{
if (isupper(input[i]) || islower(input[i]) || isdigit(input[i]))
result = 1;
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input email contains
* underscore(_) after the at-sign or not.
*
* input[] is the input email from the user.
*
* This function returns 'result' which is 1 when underscore
* is not found after the at-sign, otherwise 0.
*
*************************************************************/
int checkUnder(char input[])
{
int i = 0; /* loop variable */
int result = 1; /* to store return value */
for (i = checkSign(input) + 1; i < strlen(input); i++)
{
if (input[i] == '_')
result = 0;
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input email contains
* only one dot after the at-sign or not.
*
* input[] is the input email from the user.
*
* This function returns 'result' which is 1 when underscore
* is not found after the at-sign, otherwise 0.
*
*************************************************************/
int checkDot(char input[])
{
int i = 0; /* loop variable */
int result = 1; /* to store return value */
for (i = 0; i < strlen(input); i++)
{
if (ispunct(input[i]))
{
if ((input[i] == '.') && (input[i+1] == '.'))
{
result = 0;
}
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input email contains
* digits after the at-sign or not.
*
* input[] is the input email from the user.
*
* This function returns 'result' which is 1 when digits
* are not found after the at-sign, otherwise 0.
*
*************************************************************/
int checkNumAfterAt(char input[])
{
int i = 0; /* loop variable */
int result = 1; /* to store return value */
int atSign = 0; /* to locate the position of '@' */
for (i = 0 ; i < strlen(input) ; i++)
{
if (input[i] == '@')
{
atSign = 1;
continue;
}
if ((isdigit(input[i])) && (atSign))
{
result = 0;
break;
}
}
return result;
}
/* public function */
/**************************************************************
*
* This function is a major validation function used to
* validate email.
* This function calls other functions to check invalid cases.
*
* email[] is the input email from the user
*
* This function returns 'validNum' which is 0 when invalid
* and 1 when valid to the function calling this function.
*
*************************************************************/
int validateEmail(char email[])
{
int checkAt = 0; /* get number of at-signs in the email */
int validNum = 0; /* to store return value */
int checkSpeci = 1; /* get status if invalid special character is found */
int checkPeriod = 1; /* get status if dot is found aside '@' */
checkString(email,&checkSpeci,&checkPeriod,&checkAt);
if (!isupper(email[0]) && !islower(email[0]) && !isdigit(email[0]))
{
printf("\t\tNot valid - must begin with alphabet or digit\n");
}
else if (checkSpace(email) == 0)
{
printf("\t\tNot valid - embedded space\n");
}
else if (checkAt != 1)
{
printf("\t\tNot valid - only one '@' sign is allowed\n");
}
else if (checkPeriod == 0)
{
printf("\t\tNot valid - periods are not allowed beside the '@'\n");
}
else if (checkTLD(email) == 0)
{
printf("\t\tNot valid - TLD does not meet the conditions\n");
}
else if (alphaNumeric(email) == 0)
{
printf("\t\tNot valid - must enter only alphanumeric character between @ and the TLD\n");
}
else if (checkUnder(email) == 0)
{
printf("\t\tNot valid - no underscore allowed after the '@'\n");
}
else if (checkDot(email) == 0)
{
printf("\t\tNot valid - must have only 1 dot after the '@' \n");
}
else if (checkNumAfterAt(email) == 0)
{
printf("\t\tNot valid - digits are not allowed after use '@'\n");
}
else if (checkSpeci == 0)
{
printf("\t\tNot valid - no special characters allowed\n");
printf("\t\texcept : '.' , '@' , '_' , '-'\n");
}
else
{
if (checkSpeci != 0)
{
validNum = 1;
}
}
return validNum;
}
/* public function */
/**************************************************************
*
* This function checks if the username contains only letters,
* digits and underscore (_) or not. Username must have 5 - 15
* characters and space is not allowed.
*
* username[] is the input username from the user.
*
* This function returns 'validNum' which is 1 when the input
* is in accurate format, otherwise 0.
*
*************************************************************/
int validateUsername(char username[])
{
int i = 0; /* loop variable */
int validNum = 1; /* return value */
if (checkSpace(username) == 0)
{
printf("\t\tNot valid - space is not allowed\n");
validNum = 0;
}
else if ((strlen(username) < 5) || (strlen(username) > 15))
{
printf("\t\tNot valid - must have 5 - 15 characters\n");
validNum = 0;
}
else
{
for (i = 0; i < strlen(username); i++)
{
if ((!isdigit(username[i])) && (!isalpha(username[i])) && (username[i] != '_'))
{
printf("\t\tNot valid - must be only any combination of letters, numbers and underscore(_)\n");
validNum = 0;
break;
}
}
}
return validNum;
}
/* private function */
/**************************************************************
*
* This function checks if the input password is 8-12
* characters long or not.
*
* input[] is the input password from the user.
*
* This function returns 'result' which is 1 when the
* password is 8-12 characters long, -1 if the password
* is less than 8 characters and 0 if the password is
* longer than 12.
*
*************************************************************/
int checkLength(char input[])
{
int result = 0; /* to store return value */
int length = 0; /* to store password length */
length = strlen(input);
if ((length >= 8) && (length <= 12))
{
result = 1;
}
else if ((length > 0) && (length < 8))
{
result = -1;
}
else
{
if (length > 12)
{
result = 0;
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input password contains
* at least one uppercase alphabet or not.
*
* input[] is the input password from the user.
*
* This function returns 'result' which is 1 when the
* password contains at least one uppercase letter,
* otherwise 0.
*
*************************************************************/
int checkUpper(char input[])
{
int i = 0; /* loop variable */
int result = 0; /* to store return value */
for (i = 0; i < strlen(input); i++)
{
if (isupper(input[i]))
{
result = 1;
break;
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input password contains
* at least one lowercase alphabet or not.
*
* input[] is the input password from the user.
*
* This function returns 'result' which is 1 when the
* password contains at least one lowercase letter,
* otherwise 0.
*
*************************************************************/
int checkLower(char input[])
{
int i = 0; /* loop variable */
int result = 0; /* to store return value */
for (i = 0; i < strlen(input); i++)
{
if (islower(input[i]))
{
result = 1;
break;
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input password contains
* at least two digits or not.
*
* input[] is the input password from the user.
*
* This function returns 'result' which is 1 when the
* password contains at least two digits, otherwise 0.
*
*************************************************************/
int checkPWDigit(char input[])
{
int result = 0; /* to store return value */
int count = 0; /* to count number of digits found */
int i = 0; /* loop variable */
for (i = 0; i < strlen(input); i++)
{
if (isdigit(input[i]))
{
count++;
}
}
if (count >= 2)
{
result = 1;
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input password contains
* at least one valid special character or not.
*
* valid special characters : ? , @ , % , $ , #
*
* input[] is the input password from the user.
*
* This function returns 'result' which is 0 when no
* special character is found, -1 if the password contains
* invalid special character, otherwise 1.
*
*************************************************************/
int checkSpecial(char input[])
{
int result = 1; /* store return value */
int check = 0; /* track if that letter is punctuation mark */
int count = 0; /* count invalid special character */
int punct = 0; /* count valid special character */
int i = 0; /* loop variable */
for (i = 0; i < strlen(input); i++)
{
check = ispunct(input[i]);
if (check != 0)
{
if ((input[i] != '?') && (input[i] != '@') && (input[i] != '%')
&& (input[i] != '$') && (input[i] != '#'))
{
count++;
}
else
{
punct++;
}
}
}
if (punct == 0)
{
result = 0;
}
else
{
if (count > 0)
{
result = -1;
}
}
return result;
}
/* public function */
/**************************************************************
*
* This function is a major validation function used to
* validate password.
* This function calls other functions to check invalid cases.
*
* password[] is the input password entered by the user
*
* This function returns 'validNum' which is 0 when invalid
* and 1 when valid to the function calling this function.
*
*************************************************************/
int validatePassword(char password[])
{
int validNum = 1; /* to track errors */
if (checkLength(password) == 0)
{
printf("\t\tNot valid - must not be longer than 12 characters\n");
validNum = 0;
}
if (validNum && checkLength(password) < 0)
{
printf("\t\tNot valid - must be at least 8 characters\n");
validNum = 0;
}
if (validNum && checkSpace(password) == 0)
{
printf("\t\tNot valid - embedded space\n");
validNum = 0;
}
if (validNum && checkUpper(password) == 0)
{
printf("\t\tNot valid - must contain at least one upper case letter\n");
validNum = 0;
}
if (validNum && checkLower(password) == 0)
{
printf("\t\tNot valid - must contain at least one lower case letter\n");
validNum = 0;
}
if (validNum && checkPWDigit(password) == 0)
{
printf("\t\tNot valid - must contain at least two digit\n");
validNum = 0;
}
if (validNum && checkSpecial(password) == 0)
{
printf("\t\tNot valid - must contain at least 1 valid special characters (?,@ ,%%,$,#)\n");
validNum = 0;
}
if (validNum && checkSpecial(password) == -1)
{
printf("\t\tNot valid - unknown punctuation mark found (only ?,@,%%,$,# are allowed)\n");
validNum = 0;
}
return validNum;
}
/* public function */
/**************************************************************
*
* This function checks if the gender entered by the user is
* correct or not.
*
* F - Female M - Male
*
* gender[] is the gender sent from other function.
*
* This function returns 'result' which is 1 when the input
* gender is in correct format, otherwise 0.
*
*************************************************************/
int checkGender(char gender[])
{
int result = 0; /* to store return value */
if ((!strcasecmp(gender,"M")) || (!strcasecmp(gender,"F")))
{
result = 1;
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input location exists or not.
*
* Arguments
* input[] - the input location from the user
* location - provinces/districts in the
* selected region
* locationCount - number of provinces/districts
* in the region
*
* This function returns 'result' which is 1 when input
* location found in selected region, otherwise 0.
*
*************************************************************/
int checkLocation(char input[], char location[][MAXLEN], int locationCount)
{
int i = 0; /* loop variable */
int result = 0; /* to store return value */
for (i = 0; i < locationCount; i++)
{
if (strcasecmp(input,location[i]) == 0)
{
result = 1;
break;
}
}
return result;
}
/* private function */
/**************************************************************
*
* This function passes the provinces/districts in the
* selected region to another function to check if the
* entered location exist or not.
*
* Arguments
* input[] - the input location from the user
* region - selected choice for region
*
* This function returns 'result' which is 1 when input
* location exists, otherwise 0.
*
*************************************************************/
int checkRegion(char input[], int region)
{
/* an array of string holding provinces in northern Thailand */
char northern[9][MAXLEN] = {"CHIANGMAI","CHIANGRAI","LAMPANG","LAMPHUN","MAEHONGSON",
"NAN","PHAYAO","PHRAE","UTTARADIT"};
/* an array of string holding provinces in northeastern Thailand */
char northeastern[20][MAXLEN] = {"KALASIN","KHONKAEN","CHAIYAPHUM","NAKHONPHANOM",
"NAKHONRATCHASIMA","BUENGKAN","BURIRAM","MAHASARAKHAM",
"MUKDAHAN","YASOTHON","ROIET","LOEI","SAKONNAKHON",
"SURIN","SISAKET","NONGBUALAMPHU","UDONTHANI",
"NONGKHAI","UBONRATCHATHANI","AMNATCHAROEN"};
/* an array of string holding provinces in western Thailand */
char western[5][MAXLEN] = {"KANCHANABURI","TAK","PRACHUAPKHIRIKHAN",
"PHETCHABURI","RATCHABURI"};
/* an array of string holding provinces in central Thailand */
char central[21][MAXLEN] = {"KAMPHAENGPHET","CHAINAT","NAKHONNAYOK","NAKHONPATHOM",
"NAKHONSAWAN","NONTHABURI","PATHUNTHANI","PHRANAKHONSIAYUTTHAYA",
"PHICHIT","PHITSANULOK","PHETCHABUN","LOPBURI","SAMUTPRAKAN",
"SAMUTSAKHON","SAMUTSONGKHRAM","SINGBURI","SUKHOTHAI",
"SUPHANBURI","SARABURI","ANGTHONG","UTHAITHANI"};
/* an array of string holding provinces in eastern Thailand */
char eastern[7][MAXLEN] = {"CHANTHABURI","CHACHOENGSAO","CHANBURI","TRAT","PRACHINBURI",
"RAYONG","SAKAEO"};
/* an array of string holding provinces in southern Thailand */
char southern[14][MAXLEN] = {"KRABI","CHUMPHON","TRANG","NAKHONSITHAMMARAT","NARATHIWAT",
"PATTANI","PHANGNGA","PHATTHALUNG","PHUKET","RANONG",
"SATUN","SONGKHLA","SURATTHANI","YALA"};
/* an array of string holding districts in Bangkok */
char bangkok[50][MAXLEN] = {"BANGBON","BANGKAPI","BANGKHAE","BANGKHEN","BANGKHOLAEM",
"BANGKHUNTHIAN","BANGNA","BANGPHLAT","BANGRAK","BANGSUE",
"BANGKOKNOI","BANGKOKYAI","BUENGKUM","CHATUCHAK","CHOMTHONG",
"DINDAENG","DONMUEANG","DUSIT","HUAIKHWANG","KHANNAYAO",
"KHLONGSAMWA","KHLONGSAN","KHLONGTOEI","LAKSI","LATKRABANG",
"LATPHRAO","MINBURI","NONGCHOK","NONGKHAEM","PATHUMWAN",
"PHASICHAROEN","PHAYATHAI","PHRAKHANONG","PHRANAKHON",
"POMPRAPSATTRUPHAI","PRAWET","RATBURANA","RATCHATHEWI",
"SAIMAI","SAMPHANTHAWONG","SAPHANSUNG","SATHON","SUANLUANG",
"TALINGCHAN","THAWIWATTHANA","THONBURI","THUNGKHRU",
"WANGTHONGLANG","WATTHANA","YANNAWA"};
int result = 0; /* to store return value */
switch (region)
{
case 1:
result = checkLocation(input,northern,9);
break;
case 2:
result = checkLocation(input,northeastern,20);
break;
case 3:
result = checkLocation(input,western,5);
break;
case 4:
result = checkLocation(input,central,21);
break;
case 5:
result = checkLocation(input,eastern,7);
break;
case 6:
result = checkLocation(input,southern,14);
break;
case 7:
result = checkLocation(input,bangkok,50);
break;
}
return result;
}
/* private function */
/**************************************************************
*
* This function checks if the input contains only alphabets
* or not.
*
* input[] is the input data from the user
*
* This function returns 'result' which is 1 when input
* contains only alphabets, otherwise 0.
*
*************************************************************/
int checkAlpha(char input[])
{
int i = 0; /* loop variable */
int result = 1; /* to store return value */
for (i = 0; i < strlen(input); i++)
{
if (isalpha(input[i]) == 0)
{
result = 0;
break;
}
}
return result;
}
/* public function */
/**************************************************************
*
* This function is a major validation function used to
* validate location.
* This function calls other functions to check invalid cases.
*
* Arguments
* location[] - the input location from the user
* region - selected choice for region
*
* This function returns 'validNum' which is 0 when invalid
* and 1 when valid to the function calling this function.
*
*************************************************************/
int validateLocation(char location[], int region)
{
int validNum = 1; /* to track errors */
if (checkSpace(location) == 0)
{
printf("\t\tNot valid - no space allowed\n");
validNum = 0;
}
if (validNum && (checkAlpha(location) == 0))
{
printf("\t\tNot valid - only alphabets are allowed\n");
validNum = 0;
}
if (validNum && (checkRegion(location,region) == 0))
{
printf("\t\tNot valid - unknown province/district in this region\n");
validNum = 0;
}
return validNum;
}