How to allow user only japanese numbers input for mobile field?
disabled other input.
This SO thread has almost exactly what you need.
In short, you use Regex to validate that only Japanese characters were used. You can use jquery.validate.js or write it all out yourself.
Related
I have been recently requested, to adapt an app's input, to support Unicode letters, on some of the inputs within the web app.
That app, already does some validation with regex, with the pattern html attribute. Like so:
<input required="true" pattern="[a-zA-Z0-9_\-]+" type="text" name="name">
Now, since I have to adapt some inputs to the new requirements, I was wondering what would be better to do?
Do Encoding / decoding UTF8 in javascript?
http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html
http://laffers.net/blog/2010/12/10/regex-match-unicode-characters/
or
Addapt the regex pattern just like suggested here: PHP Regex for Multiple Unicode Characters ?
Javascript is by definition completely in unicode (Except websites with non unicode encoding, but there the solution may still work), so just add letters you need to regexp. If you need to add them by charcode use \x0000
I had decided to go for editing the regex option, since when my view starts being processed, there is a module that will set the pattern attribute, with the defined regex, on specific inputs.
So, it is better to edit the regex pattern and then set in on inputs when the view is loaded, then doing:
Load view and set pattern attributes for each input
Write js to analyze specific inputs, to see if they contain decoded Unicode characters
If so, encode those characters while typing/before submit, since I have a regex pattern that doesn't allow such characters
Essentially, I'm saving me time on writing useless code, and most important, a lot of browser processing (it was going to be to much, for what it is needed).
I should've gone for this option since the beginning (duh!)
I have an HTML form I've created that has a field that needs to take a dollar amount.
From what I've read, JavaScript doesn't recognize decimals as numeric components and the accepted practice is to take input in cents rather than deal with dollar amounts.
Problem is, the form I'm writing submits to a function that reads the form input and performs some actions with them and takes this dollar amount as text in the format ##.## and will reject any input not in this exact format.
What I'm wondering is: is it possible to create a text box that has takes 5 characters and sets the third character as a permanent decimal point?
If not, is there any way I can use JS validation to ensure that the input is in the proper format?
Thanks!
I think you are going to have to validate it. This post has some good regex for validation dollar amounts: Currency validation. It also has some good points for how to trim so that a user can't enter $412.234 or anything like that.
You cannot create, in HTML, a text box that has a particular character fixed in it. There is hardly any need either, since it is more natural for a user to type 12.34 (when specifying a sum of money) than to type 1234 and have a period inserted automatically.
You can specify the required format using the HTML5 pattern attribute. It is not supported by all browsers yet, but it does no harm when not supported, and it performs the checks even when JavaScript is disabled. You should add JavaScript checking of course, and in a simple case like this, it might be better to write the code directly instead of using any library:
<input pattern="\d?\d\.\d\d" maxlength=5 size=5 onchange="check(this)">
<script>
function check(elem) {
if(!elem.value.match(/^\d?\d\.\d\d$/)) {
alert('Error in data – use the format dd.dd (d = digit)');
}
}
</script>
The regular expression used accepts both dd.dd and d.dd (omit the ? to allow the first format only).
The example uses alert for simplicity; in a real page, you should use some less disruptive way of signaling the error to the user (typically, writing text to an area reserved for such messages), but the best way to do that depends on the design of the page as a whole.
(It might seem more logical to use <input type=number min=0 max=99.99 step=0.01 ...>, but this would raise serious localization issues, and browsers implement type=number rather poorly if at all. Most importantly, there is no guarantee that the data sent would conform to the format required. E.g., in Chrome, using the controls created by the browser, the number would be stepped from 0.09 to 0.1 and not 0.10, from 0.99 to 1 and not 1.00 etc.)
I have a form and I need to require letters and numbers. All the solutions I have seen, simply allow only letters and numbers but do not require both.
I have this Regex: /^[0-9a-zA-Z]+$/ which allows one or the other. How can I make this a requirement, meaning the text must contain at least a number.
Thanks my friends.
Guy
To break this down, we're requiring at least 2 characters, a letter and a number. In the code we start with the possibility of an alpha-numeric character. I'm not using \w because it also allows _ characters. In the group we have an or that looks for either a letter before a number, or a number before a letter. Then after the group we're requiring if anything exists that it also be alpha-numeric.
/^[A-Za-z0-9]*([A-Za-z][0-9]|[0-9][A-Za-z])[A-Za-z0-9]*$/i
As a recommendation, it's always best to use a server-side language as your front-line defense when validating a form instead of a Javascript-only approach. The reasons:
Someone can disable Javascript
The server needs to be protected from malicious attack (SQL or XSS injection)
Someone can bypass your form altogether by directly linking to the handler (if you're not requiring a valid referrer)
Some browsers like Lynx do not use Javascript, so it's not user friendly for people who need to use screen reading devices
if I want to validate the input of a <textarea>, and want it to contain, for example, only numerical values, but even want to give users the possibility to insert new lines, I can selected wanted characters with a javascript regex that includes even the whitespace characters.
/[0-9\s]/
The question is: do a whitecharacter can be used to perform injections, XSS,even if I think this last option is impossible, or any other type of attack ?
thanks
/[0-9\s]/ should be a safe whitelist to use, I believe. You do need to ensure that it checks the entire input, though; I think you mean /^[0-9\s]*$/.
Also remember, of course, that you have to validate it server-side, not just in the browser. Attackers can easily bypass JavaScript validation code.
One of my project requirements is to validate a data field(text box from web page). I need to allow alpha numeric characters in all foreign languages (japanese,chinese,korean,russian,latin american characters etc). And avoid special characters.
I am using the expression /[^a-zA-Z0-9]/ (javascript, asp .net page)
Many blogs I have read and not able to correctly understand what exactly I have to do here. I can't add any addons or plugins and have to use the available functionality of regex.
How do regex engine recognize other language characters (esp. Unicode)?
You will need to use Unicode regular expressions. Please check this previous SO thread in which various unicode matching mechanisms are discussed.