CFINPUT - How to exclude O, o, I, i from input value - coldfusion

How can I force format in Coldfusion for alphanumeric characters to exclude O, o, I, i? Mask apparently doesn't work.
Update from comments:
The following example is how to force a format with characters and numbers, but it allows O & I. I would like to exclude these two.
<cfinput type="text" name="newPart" mask="EB-9999-XX-999999" />

Leigh and Dan are both correct. More info is needed, and you'll ultimately likely end up with some RegEx.
What is the end goal? Do you want to keep users from entering any character that looks like a zero or a one (my assumption)? That should probably also include L. What about 0 and 1 themselves? Do you want to change these characters as they are entered into a form field or after they are submitted by the form? If you mask them before submission, how will you let the user know they're submitting something different than what they're entering?
Based on my assumptions above, you could start with replacing them in the form field itself with Javascript.
<input name="formStr" type='text'
onKeyUp="this.value = this.value.replace(/o/ig,'0')
.replace(/l|i/ig,'1').replace(/[^0-9a-z]/ig,'')
">
Then also replace the entry before you do anything with it. I've always been a fan of using the Java String functions for string manipulation. It's very fast.
inStr = FORM.formStr.replaceAll("(?i)[o]","0")
.replaceAll("(?i)[il]","1")
.replaceAll("(?i)[^0-9a-z]","")
;
You don't want to just do the masking in the form field. Someone can still Right-Click >> Paste into that field and submit the form with invalid characters. Ctrl-V pasting does involve an onKeyUp event, so the text will get masked that way, but the Java replaceAll() takes care of it on the submission end.
Or if replacing all of the above characters with an empty string:
onKeyUp="this.value=this.value.replace(/[^0-9a-hjkmnp-z]/ig,'')"
and
inStr2 = FORM.formStr.replaceAll("(?i)[^0-9a-hjkmnp-z]","");
But again, without knowing more about how this input will be used, it's hard to give accurate help.
NOTE: The onKeyUp worked in Chrome and IE, but not in Firefox. Odd. It's been a while since I've written JS and I haven't dug too deeply into this one, so I don't know what I missed to make it fail. Perhaps someone else can shed light.
And an added FYI: Ben Nadel has a bunch of excellent information about using RegEx. And plenty of other sources. It's worth checking out.

Related

Angular Form Input block (space) REGEX

I have an input field in my Angular component in which i want to not allow a user to be able to type a (space).
I've tried using
<input type="text" [(ngModel)]="inputText" pattern="[a-zA-Z]">
which wasn't what i wanted, and it didn't work anyways!
Does anybody know what the correct regex pattern to just block the (space) key is? And what is the correct way to use the pattern, as the above pattern didn't work...
Thanks in advance.
Using RegEx will still allow the user to type in space. But it will mark the field as invald if a pattern validator is applied to it.
If you don't really want to allow the user to type in space in the first place, you'll have to prevent it by listening to the keydown event on the input and then handling it to prevent its default behaviour. Here, give this a try:
<input type="text" (keydown.space)="$event.preventDefault()">
Here's also a Sample StackBlitz for your ref.
If you want to allow any type of character except spaces alone without any letters, you can use this:
"^\w+( +\w+)*$"
If you also want to use accented vowels, you can use this:
"^[a-zA-Zá-úÁ-Ú0-9]+( +[a-zA-Zá-úÁ-Ú0-9]+)*$"
You can use the following pattern:
<input pattern="[^\s]*">
[^\s] is a negative set which matches every character which is not in the set.
\s matches a white space character (e.g. space, tab, etc.)
* matches 0 or more character of the preceding item
Here is an example of how the browser checks if the pattern is correct (i.e. Google Chrome for example does not allow you to submit the form if there is a whitespace character in it. Test it here (enter a string containing a white space and hit Submit):
<form>
<input pattern="[^\s]*">
<button type="submit">Submit</button>
</form>
The best way of addressing this problem is by writing the directive which you can use on multiple locations.
Here is the Stackblitz sample for the same

How to remove spaces from string in django template

How do you remove all spaces from a string value in a django template?
For example
<a href="www.somesite.com/lookup?id={{ order.id }}"</a>
The order number may have spaces in it and how the templates encodes them breaks the lookup function of the site. (Not our site so can't fix that end)
I'm aware that you can use order.id.strip to get rid of the spaces at the front and end but we need to remove them from the middle of the string as well.
I'm also aware that we could create a custom filter to do it but we like to avoid custom filters for one-off things like this.
From: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#cut
cut
Removes all values of arg from the given string.
For example:
{{ value|cut:" " }}
If value is "String with spaces", the output will
be "Stringwithspaces".
This worked perfectly for what I wanted. Was not easy to find so creating this question to help the google juice for the next person.
So in my example it would be:
<a href="www.somesite.com/lookup?id={{ order.id|cut:" " }}"</a>

Regular expression to remove repeated slashes that are after a specific word (VBScript/Classic ASP)

I'm struggling here, trying to figure out how to replace all double slashes that come after a specific word.
Example:
<img alt="" src="/pt/webf//2015//47384_1.JPG" height="235" width="378" />
<div>Don't remove this // or this//</div>
I want the string above to look like this:
<img alt="" src="/pt/webf/2015/47384_1.JPG" height="235" width="378" />
<div>Don't remove this // or this//</div>
Notice the double slashes have been replaced with just one slash in the img tag but left unscathed in the div tag. I only want to replace the double slashes IF they come after the word: pt.
I tried something like this:
(?=pt)((.*?)\/\/)+
However, the first thing wrong with it is (?=) does not do pattern backtracking, as far as I'm aware. That is, it'll only look for the first matching pattern. The second thing wrong with it is it doesn't work as I intended it to.
https://regex101.com/r/kC4tA5/1
Or maybe I'm going about this the wrong way, since regular expression support is not expansive in VBScript/Classic ASP and I should try to break up the string and process, instead of trying to do everything in one regular expression???
Any help would be appreciated.
Thank you.
I am interpreting your issue as "Removing repeated slashes in all <img src> attributes."
As I said in the comments, working with HTML requires a parser. HTML is too complex for regular expressions, all kinds of things can go wrong.
Luckily, there is a parser available to VBScript: The htmlfile object. It creates a standard DOM from your HTML string. So the solution becomes exactly as described:
Function FixHtml(htmlString)
Dim doc, img, slashes
Set slashes = New RegExp
slashes.Pattern = "/+"
slashes.Global = True
Set doc = CreateObject("htmlfile")
doc.Write htmlString
For Each img In doc.getElementsByTagName("IMG")
img.src = slashes.Replace(img.src, "/")
img.src = Replace(Replace(img.src, "about:blank", ""), "about:", "")
Next
FixHtml = doc.body.innerHTML
End Function
Unfortunately, htmlfile is not the most advanced HTML parser in the world, but rest assured that it will still do way better than any regex.
There are two minor issues:
I found in my tests that for some reason it insists on prepending the img.src with about: or about:blank. This should not happen, but it does. The second line of Replace() calls gets rid of the unwanted additions.
The .innerHTML will produce tags names in upper case, so <img> becomes <IMG> in the output. Also insignificant line breaks in the HTML source might be removed. This is a minor annoyance, I recommend you don't obsess over it.(*)
But there are two big plus sides as well:
The DOM puts you in a position where you can work with the input in a structured way. You can put in any number of complex fixes now that would have been impossible to do with regex.
The return value of .innerHTML is sane HTML. It will fix any gross blunder in the input and turn it into something that is well-nested, well-escaped and otherwise well-behaved.
(*) If you do find yourself obsessing over it, you can use the wisdom from this blog post to create a function that replaces all uppercase tags that come out of .innerHTML with lowercase versions of themselves. This actually is something you can use regex for ("(</?[A-Z]+)", to be exact), because we know that there will be no stray < not belonging to a tag anywhere in the string, because that's .innerHTML's guarantee. While it would be a nice exercise (and it introduces you to the little-known fact that VBScript has function pointers), I would say it's not really worth it.

Regex in angular- first digit not zero, but allow single zero

I want to have input element which allows one of two conditions:
Single zero can be entered
Number with max of 9 digits can be entered, but first digit shouldn't be zero
I wrote this regex (solution works in online regex testers):
/(^0$)|(^[1-9]\d{0,8}$)/
But when I use it in ng-pattern in Angular, it doesn't work.
Here is my plunker example:
http://plnkr.co/edit/iDQ7ly8ypJ3UmN5A0hJw?p=preview
Not sure if alternation is doing the problems, or I messed up the regex.
UPDATE: it seems that type="number" is causing problems. Unfortunately, I need to have this in my code, so I'm searching for solution which works with type="number".
This should work for you. I did the following:
Took out the type="number".
Gave the form a name.
Gave the input a name.
Referenced the form and input via their names instead of their id and ng-model values, respectively.
It converts the value to a number under the covers, stripping the leading zeros and converting text to 0, etc.. And the name is the correct way to access it as far as I can tell.
<form name="myForm">
<input name="myNumberField" ng-model="myNumber" ng-pattern="/(^0$)|(^[1-9]\d{0,8}$)/" required/>
<span ng-show="myForm.myNumberField.$error.pattern">Invalid pattern</span>
</form>
Here is a plunker for it.

How do I deal with this regex issue?

I need to extract a value from a hidden HTML field, have somewhat figured it out but I'm currently stuck.
My regex looks like:
<input type="hidden" name="form_id" value=".*"
But this extracts the whole string from the HTML.
The string looks like:
<input type="hidden" name="form_id" value="123"/>
I need to extract the "value" from the string, it is always changing, but the "name" is always the same. Is there a way to extract it without doing another expression? I appreciate any help.
(?<=<[^<>]+?name="form_id"[^<>]+value=")(.*)(?=")
I just threw this together. Basically you want to negate any ending > in your request. So you'd likely want to do something of this nature:
<[^>]*hidden[^>]*value="(.*)"[^>]*>
And then read the first capture group (Delphi instructions). This keeps it as reasonably generic as possible although it does assume positional order on "hidden" and "value".
In order to find the value without regard for order you could use could use a slightly cleaner lookahead as was suggested:
^(?=.*name="form_id").*value="([^"]*)".*$
<[a-zA-Z"= _^>]*value="(\d*)"/>
I have tested this for your example.
If you want to extract for only input tag you can write:
<input[a-zA-Z"= _^>]*value="(\d*)"/>