Reputation: 171
I have a SAPUI5 project and have an input which I'm wanting to do some validation on.
It is inputting an 8 character length entry with a mixture of numbers and letters. I have the input max length at 8 characters already.
What I need is to create a function to only allow entry of either a number or a letter at certain characters in the entry (see below).
1 - Letter 2 - Letter 3 - Number 4 - Letter 5 - Number 6 - Number 7 - Number 8 - Number
eg, BP1A8123
Is there a way to do that in JavaScript? I've come across examples of letters only or number only but can't find an example of number / letters at certain characters within the entry.
Any direction would be appreciated.
Upvotes: 2
Views: 2175
Reputation: 666
From a JavaScript perspective, you could do the check like this:
pattern = RegExp('[A-Z]{2}[0-9]{1}[A-Z]{1}[0-9]{4}')
pattern.test('BP1A8123') // true
pattern.text('B1111111') // false
From a SAPUI5 perspective, input validation is generally handled via the data binding. See, for example, this SAPUI5 Demokit Sample. Also see the validationError
and validationSuccess
events of class `sap.ui.core.Core'.
However, for your specific requirement, the sap.m.MaskInput
control may be what you're after. See this Demokit example.
Upvotes: 2
Reputation: 25
You can split the string using .split('')
(see here), parse the character into int to check if the variable is a number using isNaN
(see here).
Upvotes: -1
Reputation: 20424
So we have the template:
template = ['letter', 'letter', 'number', 'letter',
'number', 'number', 'number', 'number']
... and some input:
input = 'BP1A8123'
We can then just use isNaN()
to check whether the respective characters are letters:
input.split('').every((c,i) => isNaN(c) || template[i] == 'number')
which gives true
in this instance.
Upvotes: 0