I want to perform a field validation,but the conditions are
1)The field should have 10 character.
2)off these 1st 5 character should be alphabets and next 5 character should be numeric digits
I performed validation for maximum length check,but rest of the thing how to perform.Is that can be done on a single "if" condition.
I am searching for the logic in google for performing that,but not got any idea.Can any one help me to perform the same.
forms.py for length check
def clean_bookref(self):
cd=self.cleaned_data
bookref=cd.get('bookref')
if len(bookref)<10 and re.match(r'[A-z0-9]+', bookref):
raise forms.ValidationError("Should be 10 digits")
return bookref
I am using this code to do but it is not working.
Thanks
Perhaps you could use something like his:
def clean_bookref(self):
cd=self.cleaned_data
bookref=cd.get('bookref')
if not re.match(r'^[A-Za-z]{5}[0-9]{5}$',bookref) :
raise forms.ValidationError("Should be of the form abcde12345")
return bookref
Related
In painless I would like to create a script which reads a keyword field called 'objldn' and extracts only five consecutive characters sometimes present in a precise position. Infact, in the keyword field 'objldn' there are a large variety of long strings among which there are some of them with a third underscore. After the third underscore, if it is present, I can fetch the consecutive 5 chars.
Whith the following lines of code I implement what I want:
def LU = doc['objldn'].value.splitOnToken('_');
return LU[3].substring(0, 5);
But the system returnes an error message "out of bounds":
Request error: array_index_out_of_bounds_exception, Index 3 out of
bounds for length 3 in "def LU =
doc['objldn'].value.splitOnToken('_'); ..." (Painless script)
error executing runtime field or scripted field on index pattern
return LU[3].substring(0, 5);
^---- HERE
may be it is due to the fact that many strings do not have the third underscore or do not even have one and therefore I need to implement firstly a IF statement which evaluates if a third underscore is in the string and only if it is present it proceeds to execute splitOnToken()... but I am not able to do it correctly. Can you help me to add the IF statement in the script please?
Why not simply checking the length of the LU array?
def LU = doc['objldn'].value.splitOnToken('_');
return LU.length >= 4 ? LU[3].substring(0, 5) : null;
I have connected the sql database to my vb project and the last feature I need is to validate the user input. Two of the textboxes needs to be validated when adding a new record.
txtName is the first textbox that consists of the following format: BCO103/T1/01 . 'BCO' will always stays the same, however the rest needs to be input by the user. Letters and numbers needs to stay in exact the same place.
txtModuleID is the second textbox that needs to be validated. The data for this field looks like this: BCO120 . Yet again, BCO will always stay the same, however the 3-digit number will change.
Im sure you can use substrings for this
for example:
If txtModuleID.Text.Substring(0,2) = "BCO" And txtModuleID.Text.Substring... etc Then 'add other conditionals
blnValidated = True
Else
blnValidated = False
End If
If txtModuleID.Text.Trim.ToUpper().SubString(0,2).equals("BCO") and len(txtModuleID.Text.Trim) = 6 Then
If txtModuleID.Text.Trim.SubString(3,5).isNumeric() Then
//valid input
Else
//message prompt that last 3 digits of the input is not numeric
End If
Else
//message prompt that input has invalid format and that input must start with BCO
End IF
Substring (0, 2) means from 0 until 2... getting the sub string of the input with letters in index 0, 1 and 2. The rest follows.
As for the first input, please do expound. I didn't quite catch how you want it to be validated.
I am working on a watermarking project that embeds binary values (i.e 1s and 0s) in the image, for which I have to take the input from the user, and check certain conditions such as
1) no empty string
2) no other character or special character
3) no number other than 0 and 1
is entered.
The following code just checks the first condition. Is there any default function in Matlab to check whether entered string is binary
int_state = get(handles.edit1,'String'); %edit1 is the Tag of edit box
if isempty(int_state)`
fprintf('Error: Enter Text first\n');
else
%computation code
end
There is no such standard function, but the check can be easily implemented.
Use this error condition:
isempty(int_state) || any(~ismember(int_state, '01'))
It returns false (no error) if the string is non-empty and composed of '0's and '1's only.
The function ismember returns a boolean array that indicates for every character in int_state whether it is contained in the second argument, '01'. The advantage is that this can be generalized to arbitrary sets of allowed characters.
I think the 2nd and 3rd can be combined together as 1 condition: your input string can only be a combination of 0 and 1? If it is so, then a small trick with findstr can do that:
if length(findstr(input_str, '1')) + length(findstr(input_str, '0')) == length(input_str)
condition_satisfied;
end
tf = isnumeric(A) returns true if A is a numeric array and false otherwise.
A numeric array is any of the numeric types and any subclasses of those types.
isnumeric(A)
ans =
1 (when A is numeric).
I've recently been trying to validate a user input so that only letters from the alphabet are accepted, how would I do this? I know how to validate user input for most things, but this one line of code for letters is really troubling me.
You can check the contents of a field with this function:
function validate theString
return matchText(theString,"^[a-zA-Z]+$")
end validate
^[a-zA-Z]+$ is a regular expression. ^indicates the beginning of a string, the brackets equal one char and the expression inside the bracket determine a set of characters. The + means that all following characters have to be equal to the preceding (set of) character(s). The $ indicates the end of the string. In other words, according to this expression, all characters must be of the set a up to and including z or A up to and including Z.
matchText() is a LiveCode function, which checks if the string in the first parameter matches the regular expression in the second parameter. Put the validate() function somewhere at card or stack level and call it from a field in a rawKeyUp handler:
on rawKeyUp
if not validate(the text of me) then
beep
answer "Sorry, that's wrong"
end if
end rawKeyUp
You could also check beforehand:
on keyDown theKey
if validate(theKey) then
pass keyDown
end if
end keyDown
This method is slightly verbose. You could also put the matchText function in a keyDown handler of your field.
I'm trying to write a regex to validate a string and accepts only a series of four comma-separated digits, each up to 100. Something like this would be valid:
20,30,40,50
and these invalid:
120,0,20,0
20,30,40,ss
invalid_string
Any thoughts?
They're used for CMYK colours. We just need to store them here, not use them.
Number Range and Subroutine
In Ruby 2+, for a compact regex, use this:
^([0-9]|[1-9][0-9]|100)(?:,\g<1>){3}$
Explanation
The ^ anchor asserts that we are at the beginning of the string
The parentheses around ([0-9]|[1-9][0-9]|100) match a number from 0 to 100 and define subroutine #1
(?:,\g<1>) matches one comma and the expression defined by subroutine # 1
The {3} quantifier repeats that three times
The $ anchor asserts that we are at the end of the string
I'd save myself the headache of using regex for a number related problem. Also the validation message will look akward so it's better to make your own:
validate :that_string_has_only_4_numbers_upto_100
def that_string_has_only_4_numbers_upto_100
errors.add(:str, 'is not valid.') unless str.split(/,/).all? { |n| 1..100 === n.to_i }
end
Unless you a re regex jedi guru like #zx81 :p.
^(?:\d{1,2},){3}\d{1,2}$
Try this