-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFieldValidator.js
More file actions
47 lines (37 loc) · 1.34 KB
/
TextFieldValidator.js
File metadata and controls
47 lines (37 loc) · 1.34 KB
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
function TextFieldValidator(myTarget,myRegex,myColor) {
/**regular expression to be used */
//this.regex = /^(\d)$/;
this.regex = myRegex;
/**color of the text field if regex does not match */
this.strErrorColor = myColor;
/**text field object to be validated */
this.txtTextField = myTarget;
/**input from the text field to be checked against the regex */
this.strInput = this.txtTextField.value;
/**current border of the textfield*/
//this.border;
if(myColor == null){
this.strErrorColor = "red";
}else if(myRegex == null){
this.regex = /^(\d)$/;
}
//---------------------------------------------------------------SET/GET METHODS
this.setRegExp = function(myRegExp){
this.regex = myRegExp;
}
this.setErrorColor = function(myColor){
this.strErrorColor = myColor;
}
//---------------------------------------------------------------PUBLIC METHODS
this.check = function(){
isMatch = false;
this.strInput = this.txtTextField.value;
if (!this.regex.test(this.strInput)){
this.txtTextField.style.borderColor = this.strErrorColor;
}else{
this.txtTextField.style.borderColor = "";
isMatch = true;
}
return isMatch;
}
}