Localization of input for numbers - javascript

I started to translate English web site to other languages. Thai, Bengali...
For example Bengali language.
To get a number from JavaScript I'm doing something like that:
<p id="demo"></p>
<input type="text" id="demoin" lang="bn"></input>
<script>
var a = 12345.44;
document.getElementById("demo").innerHTML = (a.toLocaleString('bn-US'));
document.getElementById("demoin").value = a.toLocaleString('bn-US');
</script>
And I'm getting this, as expected.
১২,৩৪৫.৪৪
But how can I deal with <input type="number"> elements.
Browser does not accept Bengali numbers in these inputs.
If I use type="text" I'm getting Bengali numbers on the Server and my PHP needed to convert it to put in into MySQL database.
How do other developers deal with translations?
Is there a JS library or Browser settings to help with it?

Related

How to mask customized edit with regexp and maxlength of numbers only on Javascript?

I'm looking for an any js-lib or custom solution to customize input field. Requirements:
Input can conain only numbers, whitespace, round brackets and dots.
Maxlength should be calculated and limited by length = 10 NUMBERS ONLY. So for another symbols no limit
Tried to use imask, jquery mask plugin and others but can't find any solution.
You could solve simple client side validation with HTML5 like this
<form>
<input type="text" pattern="^[0-9().]{0,10}$" />
<input type="submit" />
</form>
But there a many ways to solve it with jQuery. One example is this validation plugin.
Another plugin could be this.

How do I use this form to take in 2 numbers and output their sum as an alert?

I am new to JavaScript, right now I'm just practicing in hope to get better. I have been given a task to use this form to take in 2 numbers and output the sum as an alert. I have tried looking around online but the way that this code is structured makes it difficult for me to understand how it is supposed to work. Can anyone assist me on this?
<form>
<input id="x" size=3> + <input id="y" size=3>
<input type=submit value=" = " onclick="calculate()">
</form>
You need to create a function called calculate in JavaScript file or within a <script> tag which job is to do that.
The idea is to get the inputs elements with id x and y, read their value, add them together and display the result in a alert call.
Good luck with your homework :)

How can I block html and urls in my input form [duplicate]

This question already has answers here:
Best way to defend against mysql injection and cross site scripting
(4 answers)
Closed 7 years ago.
I have a website that has an input form that submits to a php page and adds a players username to a database and counts the views of that player's name to a top 10 list.
My friend tried out inputting other stuff such as html code and javascript.
it get's displayed on my top 10 list.
do you have any suggestions how I can make my form more secure?
I have been searching for ages and haven't found anything yet.
all help would be highly appreciated :)
<form method="get" action="player.php">
<div class="form-group">
<div class="input-group input-group-lg">
<input name="user" type="text" class="form-control" placeholder="Steve" aria-describedby="sizing-addon2">
<span class="input-group-btn">
<input type="submit" class="btn btn-success" value="View Skin">
</span>
</div>
</form>
Freeze your requirement around username e.g. alphanumeric including special chars and Max length etc. Write validation logic using javascript regular expression. Reject everything else that fails.
Client side (HTML/Javascript) validation is only to be nice and responsive to the user, it can always be circumvented by a malicious user. Any validation needed should be done server side even if it repeats validation done on the client side.
Do not allow user to input special characters is the best way to block it. Well to be more secure and confident always have a limited set of valid characters for each text box and validate them on client-side as well as on the server side.
One example would be
function validateUsername(){
var username = document.getElementById('txt_username').value;
if( /[^a-zA-Z0-9]/.test( username ) ) {
alert('Input is not alphanumeric');
return false;
}
return true;
}
On server side you can also use regular expression. like following
if(preg_match('/[^a-z_\-0-9]/i', $username))
{
echo "not valid string";
}

Send multiple SMS text messages in Phonegap or Cordova

http://javatechig.com/phonegap/phonegap-sms-plugin-android
Please, this is very important, so important I will set a bounty to the person who answers this question the best.
Anyways, with the script in the URL in the provided, how would I set this so that you could send the same message to many text addresses, multiple text addresses but not in a group message header; in other words, send the same message separately to many different people.
Thanks so much :)
Note that I would be cool with having different input fields, which you could enter phone numbers in.
Markup:
<div>
<input type="tel" id="phoneNumbers" />
<input type="text" id="message" />
</div>
JS:
$(function () {
var message = $('#message').val();
$.each($('#phoneNumbers').val().split(/[ ,]+/)), function (phoneNumber) {
SmsPlugin.prototype.send(phoneNumber, message, '');
});
});
The phoneNumbers field supports separating phone numbers by commas, whitespace, and comma + whitespace. For example this should work:
111111111, 222222222,333333333 444444444, 55555555

Input for percentage values

I have some inputs on my webapp where we're asking the user to enter percentage values (e.g 10.75%, 23%). Currently, they have to enter these values as decimal (e.g. 0.1075 , 0.23).
This is causing confusion for some users, so I was hoping there was a way I could convert the values to and from these formats (the backend has decimal values, the HTML inputs display the percentage values). Is there a solution to this without having to roll-my-own with javascript or the like?
I didn't see anything in the HTML5 inputs that would do this, and I wasn't able to find a javascript lib that would do it easily.
What way have other people been doing percentage inputs in HTML?
Additional Info
The webapp is using Java/Spring, so the values coming to the HTML page are coming string from the java objects in question, so ideally I'm looking for something that doesn't take any backend coding.
Answer
It turns out that Spring 3.0 will do this automatically. Annotating the fields in question with:
#NumberFormat(style = NumberFormat.Style.PERCENT)
private BigDecimal rate;
Will do all the conversion to and from the input field.
The javascript required is very simple. For example you could store the fraction value of the user's input in a hidden field when the user submits.
​<form id="myForm">
<input type="text" id="myPercent" name="myPercent" />%
<input type="hidden" id="myFraction" name="myFraction" />
<input type="submit" />
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
<script type="text/javascript">
document.getElementById('myForm').onsubmit = function() {
document.getElementById('myFraction').value =
document.getElementById('myPercent').value / 100;
}
</script>
It's simple math... 75 to .75 is 75/100. Include a line which converts the numbers after they submitted them.

Categories