I am trying to validate an editable text field (“Workstation”) that should contain only 1 letter (Lets say “W”) followed by any variation of numbers (might be 4 or 400 for example).
The problem I’m having is, since it has to be a text field to allow the first character as a letter (“W”) I cant figure out how to test all the rest of the characters to make sure they are only numbers to prevent entries such as W400W.
Here my code thus far in the Input Validation of my field:
tmp := Workstation;
tmp2 := @RightBack(tmp;1);
@If( !@Contains( tmp2 ;“0”:“1”:“2”:“3”:“4”:“5”:“6”:“7”:“8”:“9”) ; @Failure(“Workstation must contain the following format example: W456” ) ; @Success)
I want “W” + “any number” to pass validation, but not more than 1 letter, so “W” + Num + “W” should not pass. Using my above formula if W456W is entered it will pass validation because it is only testing if it contains numbers.
I know I could go through and test for every letter that I want to return an error such as a,b,c,d,!,@,#,$,etc but there has got to be a better way? Are there formulas that exist such as “A-Z” or something similar?
Maybe there is a way to write @CanOnlyContain(@Rightback(tmp1;“0”:“1”:“2”:“3”:“4”:“5”:“6”:“7”:“8”:“9”))
This one is over my head, please help!
Subject: Validating a Text Feild containing text & numbers.
It’s simple. Try the below code:
TITLE := “Validation Error”;
FMT := “W{0-9}”;
MISMATCH := “WorkStation Number is not in correct format”;
FMTHELP := “It should be in the format :” + FMT+ @Char(13) + “Ex : W234”;
@If( !@Matches(WorkStation;FMT);
@Return( @Prompt([Ok]; TITLE; MISMATCH + @Char(13) + FMTHELP) : @Command([EditGotoField]; "WorkStation"));
@Success)
Subject: RE: Validating a Text Feild containing text & numbers.
Thanks AdiBabu, I think I’m much closer now but still some issues to work out.
I guess I cant use @Command’s on a input validation so I had to remove “@Command([EditGotoField]; “WorkStation”)” from your suggestion? No biggie.
If I enter “W234” as the @Prompt tells me too, it still doesn’t pass the validation. But if I enter “W2” it works fine. Maybe the temporary variable (FMT := “W{0-9}”) only works if you have “W” followed by a single digit?
I need it to work with either a 4 or 400, how can I modify this to work? I tried (FMT := “W{0-9}{0-9}{0-9}”) which works great for three digits, but I cant use 004 and it doesn’t work on a single or double digit.
Any further help would be greatly appreciated!
Subject: RE: Validating a Text Feild containing text & numbers.
Hi Jeremy,I copied that code from one of my databases and customized it according to your situation. Here’s updated code:
FMT1 := “W{1-9}”;
FMT2 := “W{1-9}{0-9}{0-9}”;
ERRMISMATCH := “WorkStation Number is not in correct format. It should be in the format of either W4 or W400.”;
@If( !@Matches(WorkStation;FMT1:FMT2);@Failure(ERRMISMATCH);
@Success)
Subject: RE: Validating a Text Feild containing text & numbers.
Worked perfect, I knew you were close! :o)
Thanks again!
Subject: Validating a Text Feild containing text & numbers.
How about
tmp := Workstation;
tmp2 := @RightBack(tmp;1);
@If( !@IsNumber(tmp2);@Failure(“Workstation must contain the following format example: W456” ) ; @Success)
Subject: RE: Validating a Text Feild containing text & numbers.
Hmm, returns the @failure statement no matter what is entered? I might be wrong but I think its because the field is a text field and not a number field?
Subject: RE: Validating a Text Feild containing text & numbers.
Ok, here’s some script for your QuerySave event that will do the same testing. YOu could always dress it up to do some additional validations.
ws$ = Trim$(Source.FieldGetText(“Workstation”))
If ws$ <> "" Then
IsOk% = True
For j% = 2 To Len(ws$)
If Instr("0123456789",Mid$(ws$,j%,1)) = 0 Then
IsOk% = False
Exit For
End If
Next
If IsOk% = False Then
Msgbox "Workstation must contain the following format example: W456"
Continue = False
End If
End If
Subject: Validating a Text Feild containing text & numbers.
Try this …
pat := “{A-Z}+{0-9}”
@If(
@Matches(Workstation;pat);@Success;
@Failure("The Workstation number must be a single letter followed only by digits." )
)
That means that the first character must be A through Z follow by any number of characters of 0 through 9.
If you need at least one number after the letter, the pattern would be
pat := “{A-Z}{0-9}+{0-9}”
If you need to support lower case …
pat := “{a-zA-Z}+{0-9}”
Subject: Validating a Text Feild containing text & numbers.
Sometimes it’s worthwhile remembering that we are using Notes 6+. Use the form’s onsubmit event, JavaScript, and a regular expression:
var val = document.forms[0].Workstation.value;
var re = /^W\d+$/i;
if (re.test(val)) {
return true;
}
else {
alert('Not a valid workstation id.\nThe required format is \"W123\"');
return false;
}
Returning false in the onsubmit will stop a save. The regular expression here is:
/^W\d+$/i
“/” = “start RegEx object”
“^” = “starts with”
“W” = “W”
“\d” = “any digit 0 to 9”
“+” = “the last thing I mentioned has to occur at least once”
“$” = “end of the string to test”
“/” = “end regex object”
“i” = “make it case-insensitive”
So the code requires that the field value start with a “W” or “w” followed by at least one digit, with no other characters allowed.