Input Number Type Dollar Sign - javascript

I have a form with a line that asks for a dollar amount, however if the user puts a dollar sign in front of the number the form won't let it send because it is set to type="number".
Is there a way to let a user type in a dollar sign ($) in the type="number" and have the form send with no issue?
Code:
<label>Monthly Budget</label>
<input name="budget" type="number" placeholder="$400.00" required data-errormessage-value-missing="Uh oh, somethings wrong!" data-errormessage-type-mismatch="Uh oh, somethings wrong!">

Just denote the symbol before the input field so they don't add it again. Currency symbols aren't handled in code, they're added to the viewport for the users display on-the-fly.
<label>Monthly Budget</label>
<span class="input">$<input name="budget" type="number" placeholder="400.00" required data-errormessage-value-missing="Uh oh, somethings wrong!" data-errormessage-type-mismatch="Uh oh, somethings wrong!"></span>
Example
input type=number - Reference

The only way to let a user type in a dollar sign in a type="number" field and have the form send with no issue is to intercept (with JavaScript) user input with keyboard event handlers such as onkeyup, remove the dollar sign, and clear the status with setCustomValidity(''). This would require some care, since keyboard event handling (and recognizing which character was entered) varies across browsers. More importantly, it would result in poor usability: the user could type “$42” but would see the “$” vanish, and could get very confused.
Putting the dollar sign in front of the field is one way (not completely reliable) of avoiding the problem of a user-entered dollar sign (rather than solving it). Note that there are other issues with the code, too, such as the use of a specific amount of money as a placeholder (use value if you wish to set an initial value) and not allowing any cents, contrary to what 400.00 suggests. A better idea:
<label for="budget">Monthly Budget</label>
<input id="budget" name="budget" type="number"
placeholder="xxx.xx"
required
step="0.01"
title="Amount of money in numbers">
(If you wish to disallow cents, which would be sensible for a budget, omit the step attribute, defaulting it to 1, and omit the placeholder attribute, since there’s probably no suitable value for it.)
Alternatively, use input type="text" and a suitable pattern attribute, e.g.
<input id="budget" name="budget" type="text"
placeholder="xxx.xx"
required
title="Amount of money in numbers"
pattern="\$?(\d)+(\.\d\d)?">
(to allow cents but not require them). Note that this would make (on supporting browsers) input like 400.000 or 400.5 invalid, which is probably good. This approach would imply that in form data handling, you would need to deal with an optional leading “$”, which is normally simple of course.

Related

How to input digits and symbols in Tizen web app

I'm trying to make input field, where I can enter IP address. If I use type="number" it is not possible to pick . symbol. But if I use type="text" there are some unnecessary keyboards are displayed, such as letters, voice input, emojis that I don't want to have. I want to have something like two pages with digits and symbols. Any suggestions?
index.html
<div class="ui-page" id="page-text-input">
<div class="ui-content text-input-content">
<input id="ip" name="ip" placeholder="IP" type="number" />
</div>
</div>
I verified your issue. I think this is not issue of keyboard or platform. Referring to https://developer.mozilla.org/pl/docs/Web/HTML/Element/Input#attributes
the input field type="number" doesn't fit to IP address. This type is dedicated for floating numbers.
Apart from the fact that there is one separator for decimals in the number, it can be a point or a comma depending on the location / region settings.
Only solution what I see that you add four input fields with type number for each IP number class or use some widget like spin widget.
Best regards,
Tomasz

How to set maximum number of characters in number type textbox?

I have one textbox which is of number type. I want it to accept maximum of 16 digits, not more than that. For that I have tried with "maxlength" attribue and "max" attribute both. But its not working. Can anyone provide me solution?
<input type="number" id="dummy" max="16" />
Edit: I need to enter only whole number since it is used for account number of bank.
There are a few of these "how do I further control type="number" fields" questions here on SO. It boils down to:
You make it a type="number" and accept that you can't further mask the input vs. what the browser does to implement a numeric input, or
You make it type="text" and use pattern and/or maxlength to enforce the specific pattern you want, and accept that the browser won't add a specific UI for numbers.
Input type number doesn't support "maxlength" attribute. You can specify "max" attribute (HTML5), which is the highest number user can input. Something like this: <input type="number" max="999999999999" id="dummy" step="1" min="0">.
Edit: Also specify step attribute to 1.

AngularJs-Facing issue with ui-number-mask in Portuguese (Brazil) locale

My application locale is Portuguese (Brazil). In Brazil a dot (.) is used to separate thousands and comma(,) is used for decimal. Suppose user enters value 4.878 When user saves the data, it saves the correct value as entered in UI, but when next time page render it shows the round off value(i.e. 5 instead of 4.878). I am facing problem with numeric text box without decimal places,
<input class="inputbox" ng-model="value" ui-number-mask="0" />
If I set 2 decimal places for same textbox, then it will show/renders correct value always(i.e. 4.878,00):-
<input class="inputbox" ng-model="value" ui-number-mask="2" />
Any suggestion, why values are rounding off and how to fix this.
This sounds like a localization issue. Have you tried following this? https://docs.angularjs.org/guide/i18n

HTML Field Type Input Number Only or Letter Only Without JavaScript

I create a form, where one field should only be filled with numbers.
I want that field can be filled only with numbers since entering input.
Like this example :
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
I've tried using Regex, but when I try to input is still able to enter letters.
<input type="number" min="2" pattern="^[0-9]" class="andi_input required-entry" name="amount" id="amount" required title='Only Number' />
I want it when input to field and not after click the Submit button and the message appear and inform that the field can only be filled with numbers.
I also try to add validate-number, but the result is the same.
How, without javascript, so that the field can only be filled with numbers or letters?
Whether for this kind of case have to use JavaScript or is there another way without JavaScript?
HTML 5 uses the type="number" so make sure that the browser that you are using is compatible. Check it out in action here.
You should check browser compatibility.
You're right with <input type="number" pattern="^[0-9]" />.
Your regex rule is :
^[0-9] :
^ assert position at start of the string
[0-9] match a single character present in the list below
(0-9 a single character in the range between 0 and 9)
You can check your regex here.
I use to use HTML5 Validation and jQuery one because IE is capricious most of the time.
UPDATE :
Without Javascript it's not possible to check pattern on real time. I suggest you to use a jQuery library like : http://www.jqueryscript.net/form/jQuery-Plugin-For-Formatting-User-Input-with-Specified-Pattern-formatter-js.html.
Here is a OneLiner:
<input type="number" onkeypress="return /[0-9]/i.test(event.key)">
or
<input type="text" onkeypress="return /[a-z]/i.test(event.key)">

Shipping details information validation in form

I am using required attribute to check the pattern on my form which get's the user's shipping info etc. I am running into 2 problems now. The required attribute does''t work in Safari or old IE. Also in the address pattern, if there is a "." lets say instead of North, user types in "N.". It comes up as invalid pattern. Is it better to use javascript to validate it? Also what other attributes can I use within my HTML to validate it because I believe pattern doesn't require user to have javascript enabled.
Here is the HTML part of my code in my form:
<input type="text" name="first_name" required pattern="[a-zA-Z]{1,}" size="30"/>
<input type="text" name="last_name" required pattern="[a-zA-Z]{1,}" size="30"/>
<input type="text" name="address1" required pattern="[a-zA-Z0-9 ]{1,}" size="30"/>
<input type="text" name="address2" pattern="[a-zA-Z0-9 ]{1,}" size="30"/>
<input type="text" name="city" required pattern="[a-zA-Z]{1,}" size="30"/>
<input type="text" name="zip" required pattern="[a-zA-Z0-9]{1,}" size="30"/>
<input type="email" name="email" required size="30" value=""/>
<input type="text" name="telephone" required pattern="[0-9]{9,}" size="30"/>
<input type="submit" id="checkout" name="checkout" class="button" value="Checkout" />
You should use javascript for checking what's inside or simply tell the user you're requiring him to type "North" instead of "N.". Users don't really mind those restrictions. For what regards the required attribute: use javascript aswell
Validating names/email/addreses using javascript's [a-zA-Z] (or \w) is a Bad Idea ™
You are making some pretty limiting assumptions about a valid users name (all tough, even Unicode can not represent everybody's name..).
Have a look (for example) here: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
For example, what about a chinese/arabic/russion/etc woman's last name who married an american (within your target-region) (think even bi-di text..).
Also, an input type 'email' is (as you have found out) new in html5. You can provide a fall-back (shim/polyfill/creation of your own) to validate the email. However.. validating email was already HARD to do and that has become worse (if not impossible without requiring frequent updates to the routine) now with all the (new) internationalized (top level) domain-names (for which the list is still growing). Knowing this, ultimately leads to the notion that even a shim/polyfill (external library) is going to have a hard time to correctly validate valid email-addresses.
Finally (since you also want to cater to Canada as you told in the chat), addresses:
what about: Côte Saint Luc, Quebec, Canada (just an example)?
Am I serious?
Yes, Depending on what you are coding for (region/audience/level of importance).
If you are fine with it, and understand the (very real possible) problems, then everything is fine.. as long as you are not using this for some legit government tax-system etc ... Or an airline booking website... etc.
However... the solution is actually simple:
be relaxed about what you accept (client-side validation shouldn't be necessary for such fields),
use Unicode for all localized fields (that includes email, and address and name) (accept that you can not solve the problem that Unicode can not represent everybody's name, but in the future that is fixed, for example China will not accept those characters anymore for newborns since (I think) 2012),
Just make sure there's no malignant code on the server-side!
Edit: you must sanitize every input on the server-side anyway (never trust on javascript for that), meaning that you also should have the code/routines to handle input that your server-side script deemed invalid (something you could communicate to the user using ajax etc. and optionally provide a fallback (full page-refresh) when javascript is disabled).
Having all this in place, kind of provides you with the 'bonus-feature' that the cross-browser issues you are running into are no longer your problem, you can/should use the new features as icing on the cake: are required values empty or not, does the email address contain an '#', do the strings contain illegal 'Control Codes'/illegal/reserved characters, etc? These features are relatively easy and reliable to shim (just an example: http://html5please.com/#polyfill) if you should want fallback-mechanisms!
Do NOT forget these 'rules' (where relevant) when handling/passing/storing this data (you wouldn't be the first to accidentally stuff your Unicode data in a database in a localized transcoded code-page)!
Hope this helps!

Categories

Resources