Quasar input will only be valid for characters and numbers - regex

<q-input
dense
type="textarea"
v-model="testData.content"
label="content"
:rules="val => val.match('^[a-zA-Z0-9_.-]*$') || 'test content can only apply letters and numbers.'
/>
I want q-input to accept only characters and numbers. I found a solution with rules, but it doesn't seem like a good idea. How can I do it in a better way so that q-input only accepts character and numbers values?

Related

Can you restrict two characters based on their ASCII order in regex?

Let's say I have a string of 2 characters. Using regex (as a thought exercise), I want to accept it only if the first character has an ascii value bigger than that of the second character.
ae should not match because a is before e in the the ascii table.
ea, za and aA should match for the opposite reason
f$ should match because $ is before letters in the ascii table.
It doesn't matter if aa or a matches or not, I'm only interested in the base case. Any flavor of regex is allowed.
Can it be done ? What if we restrict the problem to lowercase letters only ? What if we restrict it to [abc] only ? What if we invert the condition (accept when the characters are ordered from smallest to biggest) ? What if I want it to work for N characters instead of 2 ?
I guess that'd be almost impossible for me to do it then, however bobble-bubble impressively solved the problem with:
^~*\}*\|*\{*z*y*x*w*v*u*t*s*r*q*p*o*n*m*l*k*j*i*h*g*f*e*d*c*b*a*`*_*\^*\]*\\*\[*Z*Y*X*W*V*U*T*S*R*Q*P*O*N*M*L*K*J*I*H*G*F*E*D*C*B*A*#*\?*\>*\=*\<*;*\:*9*8*7*6*5*4*3*2*1*0*\/*\.*\-*,*\+*\**\)*\(*'*&*%*\$*\#*"*\!*$(?!^)
bobble bubble RegEx Demo
Maybe for abc only or some short sequences we would approach solving the problem with some expression similar to,
^(abc|ab|ac|bc|a|b|c)$
^(?:abc|ab|ac|bc|a|b|c)$
that might help you to see how you would go about it.
RegEx Demo 1
You can simplify that to:
^(a?b?c?)$
^(?:a?b?c?)$
RegEx Demo 2
but I'm not so sure about it.
The number of chars you're trying to allow is irrelevant to the problem you are trying to solve:
because you can simply add an independent statement, if you will, for that, such as with:
(?!.{n})
where n-1 would be the number of chars allowed, which in this case would be
(?!.{3})^(?:a?b?c?)$
(?!.{3})^(a?b?c?)$
RegEx Demo 3
A regex is not the best tool for the job.
But it's doable. A naive approach is to enumerate all the printable ascii characters and their corresponding lower range:
\x21[ -\x20]|\x22[ -\x21]|\x23[ -\x22]|\x24[ -\x23]|\x25[ -\x24]|\x26[ -\x25]|\x27[ -\x26]|\x28[ -\x27]|\x29[ -\x28]|\x2a[ -\x29]|\x2b[ -\x2a]|\x2c[ -\x2b]|\x2d[ -\x2c]|\x2e[ -\x2d]|\x2f[ -\x2e]|\x30[ -\x2f]|\x31[ -\x30]|\x32[ -\x31]|\x33[ -\x32]|\x34[ -\x33]|\x35[ -\x34]|\x36[ -\x35]|\x37[ -\x36]|\x38[ -\x37]|\x39[ -\x38]|\x3a[ -\x39]|\x3b[ -\x3a]|\x3c[ -\x3b]|\x3d[ -\x3c]|\x3e[ -\x3d]|\x3f[ -\x3e]|\x40[ -\x3f]|\x41[ -\x40]|\x42[ -\x41]|\x43[ -\x42]|\x44[ -\x43]|\x45[ -\x44]|\x46[ -\x45]|\x47[ -\x46]|\x48[ -\x47]|\x49[ -\x48]|\x4a[ -\x49]|\x4b[ -\x4a]|\x4c[ -\x4b]|\x4d[ -\x4c]|\x4e[ -\x4d]|\x4f[ -\x4e]|\x50[ -\x4f]|\x51[ -\x50]|\x52[ -\x51]|\x53[ -\x52]|\x54[ -\x53]|\x55[ -\x54]|\x56[ -\x55]|\x57[ -\x56]|\x58[ -\x57]|\x59[ -\x58]|\x5a[ -\x59]|\x5b[ -\x5a]|\x5c[ -\x5b]|\x5d[ -\x5c]|\x5e[ -\x5d]|\x5f[ -\x5e]|\x60[ -\x5f]|\x61[ -\x60]|\x62[ -\x61]|\x63[ -\x62]|\x64[ -\x63]|\x65[ -\x64]|\x66[ -\x65]|\x67[ -\x66]|\x68[ -\x67]|\x69[ -\x68]|\x6a[ -\x69]|\x6b[ -\x6a]|\x6c[ -\x6b]|\x6d[ -\x6c]|\x6e[ -\x6d]|\x6f[ -\x6e]|\x70[ -\x6f]|\x71[ -\x70]|\x72[ -\x71]|\x73[ -\x72]|\x74[ -\x73]|\x75[ -\x74]|\x76[ -\x75]|\x77[ -\x76]|\x78[ -\x77]|\x79[ -\x78]|\x7a[ -\x79]|\x7b[ -\x7a]|\x7c[ -\x7b]|\x7d[ -\x7c]|\x7e[ -\x7d]|\x7f[ -\x7e]
Try it online!
A (better) alternative is to enumerate the ascii characters in reverse order and use the ^ and $ anchors to assert there is nothing else unmatched. This should work for any string length:
^\x7f?\x7e?\x7d?\x7c?\x7b?z?y?x?w?v?u?t?s?r?q?p?o?n?m?l?k?j?i?h?g?f?e?d?c?b?a?`?\x5f?\x5e?\x5d?\x5c?\x5b?Z?Y?X?W?V?U?T?S?R?Q?P?O?N?M?L?K?J?I?H?G?F?E?D?C?B?A?#?\x3f?\x3e?\x3d?\x3c?\x3b?\x3a?9?8?7?6?5?4?3?2?1?0?\x2f?\x2e?\x2d?\x2c?\x2b?\x2a?\x29?\x28?\x27?\x26?\x25?\x24?\x23?\x22?\x21?\x20?$
Try it online!
You may replace ? with * if you want to allow duplicate characters.
ps: some people can come up with absurdly long regexes when they aren't the right tool for the job: to parse email, html or the present question.

How can I use REGEX to format 'phone numbers?

I have two 'phone numbers listed in different formats, but want just to extract the digits only:
17347545296
(734) 754-5296
Also using REGEXREPLACE but avoids errors where "A1" is Number format and a simpler expression:
=regexreplace(A1&"","\D","")
One approach would be to use REGEXREPLACE and remove all non numeric characters:
=REGEXREPLACE(A1, "[^0-9]+", "")
If a given cell could also have digits not belonging to the actual phone number, then we would have to do more work. But the above suggestion should work for your sample data shown.

Knockout Js Regex no negative numbers

I'm trying to find a regex code to disable negative numbers for the user input.
I'm playing around with the code abit trying to find the right one but haven't had much success.
my current code is:
Price: ko.observable().extend({
required: true,
pattern: '^[0-9].$'
})
In such case, Why do you need to allow user to enter minus numbers in your input field and validate the input against negative number?
Instead you can prevent the User from entering negative numbers/strings.
This uses JavaScript, but you don't have to write your own validation routine. Instead just check the validity.valid property. This will be true if and only if the input falls within the range.
Solution 1:
<html>
<body>
<form action="#">
<input type="number" name="test" min=0 oninput="validity.valid||(value='');">
</form>
</body>
</html>
Solution 2:
The below solution supports to validate multiple inputs.
// Select your input element.
var numInput = document.querySelector('input');
// Listen for input event on numInput.
numInput.addEventListener('input', function(){
// Let's match only digits.
var num = this.value.match(/^\d+$/);
if (num === null) {
// If we have no match, value will be empty.
this.value = "";
}
}, false)
<input type="number" min="0" />
Solution 3:
I haven't tested the below solution, But this might help as well...
Either '/^\d+$/' OR '^\d+$' pattern may help you along with your current approach.
Price: ko.observable().extend({
required: true,
pattern: '/^\d+$/'
})
Original Solution and Reference here..
Hope this helps...
You could use the digit group \d
pattern: '^\d+\.?$'
This matches the following:
The number must start at the beginning of the line
Must consist of 1 or more digits
Can have the character "." 0 or 1 times
The number must end at the end of the line
Here are some examples of matches: "34", "5", "45687654", "1.", "198289."
I notice that you said you wanted to avoid negative numbers, your solution was to squish the number to the beginning and end of the line. You can also use a negative lookbehind to check that the number does not have a negative sign, such as with
pattern: '(?<!-)\b\d+\.?'
I also added a word boundary check (\b) so that is would not try to match the 23 in -123

SAS compress: keep numbers before |

So one of my variables was coded in a messy mix of numeric values, texts, parenthesis and so on. I actually only need to extract the numeric values which are recorded as 12345 (for example, not limited to a specific number of digits, i mean it could be a n-k-digit to n-digit) followed by || and then description that might also contain some numeric values. So when I applied SAS compress funtion newvar = compress(oldvar, '', 'a'), the newvar extracted ALL the numbers from the oldvar. Thus it looks like 12345|||(789)|| etc. The number of '|' sign (which is control character to indicate line breaks etc.?) varies though.
I only need to extract the first numeric values before the '|' sign. Any help please?
Thanks in advance.
Use the SCAN() function to extract the values. It will result in a character value and converting to a numeric should be straightforward.
new_var = input(scan(old_var, 1, "|"), best12.);
This should do it:
substr("12345||45||89||...",1,find("|","12345||45||89||...",1)-1)

Regular expression to allow only negative number

This sounds like it would be a fairly common issue, but I've searched a lot and can't find anything that works. I'm using the jquery validate plug-in to validate form input. There's a field which needs to accept only negative numbers (can be whole integers or decimals but must be less than zero).
I've tried doing something like this:
$.validator.addClassRules("negativeNumber", { range: [-1000000000, 0] });
...which works okay, but the validation error is horrible ("Please enter a number between -1000000000 and 0") - I'd like it to just say "Please enter a negative number".
Could anyone point me in the direction of a regex that only allows negative numbers?
Many thanks folks...
Instead of a regex, you can add a custom validation method, which I believe is easier to read.
$.validator.addMethod('negativeNumber',
function (value) {
return Number(value) < 0;
}, 'Enter a negative number');
I would think \-\d*(\.?\d+) would do it.
\- matches a minus
\d* matches zero or more digits (to account for things like -.5)
\.? matches a decimal point if it's there
\d* matches zero or more digits
This will match anything less than 0 \-\d*(\.\d+)?
Including -.00001, -100000, and -1
This would be so much easier with a custom validation method:
jQuery.validator.addMethod('negativeNumber', function(value, element) {
return this.optional(element) || (+value < 0);
}, 'Please enter a negative number');
That is much nicer (and probably quicker) than regex, and let's the browser parse the number for you, rather than doing it yourself with regex.
Or, of course, use the max method, although the message there isn't quite as nice.