using javascript how to check string contains hindi font? - javascript

I am uploading a file using input type file I want to check that the name contains hindi font or english font.
Below is my code.
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$("document").ready(function () {
$("#upload").change(function () {
var filename = $(this).val().replace(/^.*[\\\/]/, '')
alert(filename);
});
});
</script>
</head>
<body>
<div>
<input type="file" name="upload" id="upload" value="upload" />
</div>
</body>
</html>

Hindi characters' character code is from 2309 to 2361.
try this
function hasHindiCharacters(str)
{
return str.split("").filter( function(char){
var charCode = char.charCodeAt(); return charCode >= 2309 && charCode <=2361;
}).length > 0;
}
You can use this method
hasHindiCharacters("अasd"); //outputs true
hasHindiCharacters("asd"); //outputs false
DEMO
function hasHindiCharacters(str)
{
return str.split("").filter( function(char){
var charCode = char.charCodeAt(); return charCode >= 2309 && charCode <=2361;
}).length > 0;
}
console.log(hasHindiCharacters("अasd")); //outputs true
console.log(hasHindiCharacters("asd")); //outputs false

You should be able to loop through each of the characters in the file name. Within the loop, use filename.charCodeAt(index) to get the Unicode value of the character. Checking if the char value is between 0900 and 097F which contains all the characters in the Devanagari Unicode block should tell you if it's using Hindi.

Related

Prevent more than one space between words

I have a function that prevents people putting numbers or any symbol but letters onkeypress into a text box due to ongoing problems with data entry.
<td><input type="text" name="name" onkeypress="return isAlfa(event)"></td>
Now some staff for reasons unknown put two spaces between words at random times. So I need to prevent them putting more than one space between words. I want to do this in the same function, but it keeps breaking.
function isAlfa(evt) {
evt = (evt || window.event);
var charCode = (evt.which || evt.keyCode);
if ((charCode > 32)
&& (charCode < 65 || charCode > 90)
&& (charCode < 97 || charCode > 122)
) {
return false;
}
return true;
}
How can I prevent them entering more than one space between words?
Neglecting all the other helpful suggestions and comments and strictly following the OP's requirements one has to ...
Adapt the return condition in a way that takes into account if, with the current keystroke, a whitespace sequence is going to be created.
Thus one has to implement a method that determines exactly that.
There might be some possible helper methods too.
code example ...
function isWhiteSpace(char) {
return (/\s/).test(char);
}
function willCreateWhitespaceSequence(evt) {
var willCreateWSS = false;
if (isWhiteSpace(evt.key)) {
var elmInput = evt.currentTarget;
var content = elmInput.value;
var posStart = elmInput.selectionStart;
var posEnd = elmInput.selectionEnd;
willCreateWSS = (
isWhiteSpace(content[posStart - 1] || '')
|| isWhiteSpace(content[posEnd] || '')
);
}
return willCreateWSS;
}
function isAlfa(evt) {
evt = (evt || window.event);
var charCode = (evt.which || evt.keyCode);
return ((
(charCode > 32)
&& (charCode < 65 || charCode > 90)
&& (charCode < 97 || charCode > 122)
) || willCreateWhitespaceSequence(evt)) ? false : true;
}
<input type="text" name="name" onkeypress="return isAlfa(event)"/>
This code will prevent multiple white spaces, that is, it will only allow 1 space and it will prevent two or more white spaces. You can configure the amount of white space you want to deny. I am not the author of the original code, but I made the modifications to make it work properly.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Your-title</title>
<meta charset="utf-8">
</head>
<body>
<form>
<label>Name</label>
<input type="text" name="YourInputName">
</form>
</body>
</html>
<label>Name</label>
<input type="text" name="YourInputName">
<script>
var field = document.querySelector('[name="YourInputName"]');
field.addEventListener('keyup', function (event) {
var userName = field.value;
userName = userName.replace(/\s{2,}/g, ' ');
field.value = userName;
});
</script>
may be try to use something like this. on your keypress event try to check the last value by using string substr method and if that gives you a space and current code is also a Space then prevent or do whatever you want to do with input.
function checkstuff(event){
if (event.target.value.substr(-1) === ' ' && event.code === 'Space') {
console.log('space pressed in sequence');
}
}
<input type='text' onkeypress="checkstuff(event)">
Rather than listening for and evaluating each key press it would be far simpler just to react to any and all input and then sanitise whatever was entered via a RegExp.
Example:
<input type=text id=myfield />
JS:
document.querySelector('#myfield').addEventListener('input', evt => {
evt.target.value = evt.target.value.replace(/[^a-z\s]/ig, '').replace(/\s{2,}/g, ' ');
});
That first replaces all non-letters and spaces with nothing, then replaces sequences of 2 or more spaces with a single space.

How to allow only string in input text field

How to allow only string input on text_field using rails haml form.
.field
= f.label "agent_name"
= f.text_field :agent_name, :required => true,:id=>"agent_name_validation"
$("#agent_name_validation").keypress(function(event) {
var string = /^[a-z]+$/i;
if(event.which != string){
return false;
}
});
Use this script
$("#agent_name_validation").keypress(function(event){
var inputValue = event.charCode;
if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)){
event.preventDefault();
}
});
use the following Jquery function to remove number from text field
$("#agent_name_validation").keyup(function(e) {
// Our regex
// a-z => allow all lowercase alphabets
// A-Z => allow all uppercase alphabets
var regex = /^[a-zA-Z]+$/;
// This is will test the value against the regex
// Will return True if regex satisfied
if (regex.test(this.value) !== true)
//alert if not true
//alert("Invalid Input");
// You can replace the invalid characters by:
this.value = this.value.replace(/[^a-zA-Z]+/, '');
});
Try to add jQuery validation gem
https://github.com/meowsus/jquery-validation-rails
Too much easy way to put lots of validation
All validation is their ready to use, you have to just use
Their ready methods
myField: { lettersonly: true }
And it’s done..
Best of luck..
You can use RegExp.prototype​.test() to remove the entered character if that is not a string in between the regular expression [a-z]:
Notice that this will check only lowercase characters:
$('#agent_name_validation').on('input', function () {
if (!/[a-z]$/.test(this.value)) {
this.value = this.value.slice(0, -1);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="agent_name_validation" type="text">
#Suman Das
Try this code,To allow only string in input text field:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Javascript Demo</title>
</head>
<body>
<form action="/data.php" method="get">
<input type="text" name="fullName" onkeypress="return (event.charCode > 64 &&
event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)"
placeholder="Full Name">
<input type="submit">
</form>
</body>
</html>
I hope above code will be useful for you.
Thank you.

JavaScript jQuery Regexp while

Do Regular expression have power to live format
entering string in this format ( /^\d{3}\-?\d{1,13}\-\d{2}$/ ). By that I mean , when I type ( 123 ) , he automatic put (123-).
I use jQuery function which I listed it in the example below.
--Point is when user what to type his bank account I want to live format it.
---Can this job be finished by Regexp ---
HTML:
<input type="text" id="radi" />
JavaScript:
//This work on every typed character
$("#radi").bind('input', function(event) {
$("#radi").bankPlug('change', $(this).val());
});
You can use the keypress function followed by the replace function. replace can take a function as a second parameter. Here's one possible way to do it.
$("input").on("keypress", function(e) {
var inputNum = $(this).val();
if (e.keyCode != 45 && (e.keyCode < 48 || e.keyCode > 57)) {
return false;
}
$(this).val($(this).val().replace(/\d{3}/, function(n) {
return (/\d{3}-/).test(inputNum) ? n : n + "-"
}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="number" />

How to alert user that input type text detects a dot when user input?

I have this html and jquery code that alerts the user to have numbers only to input.
I want to alert the user when user inputs a dot '.' in the text field.
<html>
<body>
<div class="form-group" id="parentID">
<input class="form-control" placeholder="ID Number (10 max numbers)" id="childID" name="idnumber" type="text" maxlength="10" required autofocus>
<span id="checkID"></span>
</div>
</body>
<script>
var ID = $("#childID").val();
if (ID.indexOf(".") != -1) {
$("#checkID").html("ID number must contain numbers only");
$("#checkID").css("color", "orange");
}
$(document).ready(function() {
$("#childID").keyup(checkPasswordMatch);
});
//In this function it will disable special characters in user input but still accepts the '.' and '>' character type, what keycode is it to disable that 2 character?
$(function() {
$('#parentID').on('keydown', '#childID', function(e) {-1 !== $.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) || /65|67|86|88/.test(e.keyCode) && (!0 === e.ctrlKey || !0 === e.metaKey) || 35 <= e.keyCode && 40 >= e.keyCode || (e.shiftKey || 48 > e.keyCode || 57 < e.keyCode) && (96 > e.keyCode || 105 < e.keyCode) && e.preventDefault() });
})
</script>
</html>
I want the span id="checkID" to appear when the input field detects a dot on keyup. I tried using indexOf() but the span tag won't appear. Can anyone help me?
I have updated your code in fiddle. Please find the updated code in below fiddle link. Just added one function and updated keyup and keypress events.
$(function() {
$('#parentID').on('keypress keyup', '#childID', function(e) {
if(isNumber(e)){
$("#checkID").html("");
} else{
//e.preventDefault();
$("#checkID").html("ID number must contain numbers only");
$("#checkID").css("color", "orange");
}
});
});
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode <= 46 || charCode > 57)) {
return false;
}
return true;
}
See the Demo
It shows the error message. If you don't want to allow other characters please uncomment //e.preventDefault();
try this this check it real time
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<style type="text/css">
</style>
<body>
first name :<input type="text" name="firstname" class="tfield" id="myinput">
</body>
<script type="text/javascript">
$(document).ready(function(){
$("#myinput").on('change keyup keydown',function(){
var thetext = $(this).val();
if (thetext.indexOf('.') != -1)
{
alert("contains dot sign")
}
else
{
//no need
}
});
});
</script>
$("#childID").keyup(function(){
if ($("#childID").val().indexOf('.') > -1) {
alert('dot');
}
});
Working Demo 1
Working Demo 2
Just write one javascript function which will check the charCode. The keyCode for dot is 46. so javascript function may like this
$('#childID').keypress(function (e) {
//if the letter is dot
if (e.which == 46) {
//display alert error message
alert("you cant enter dot");
$(this).focus();
return false;
}
});
This will not let the user type dot in the text field.
Demo:
<!DOCTYPE html>
<html>
<title></title>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<style type="text/css">
</style>
<body>
Enter text :<input type="text" name="childID" id="childID">
</body>
<script type="text/javascript">
$(document).ready(function(){
$('#childID').keypress(function (e) {
//if the letter is dot
if (e.which == 46) {
//display alert error message
alert("you cant enter dot");
$(this).focus();
return false;
}
});
});
</script>

html textbox limitation on decimal input

I have an html text box where I want to limit the numbers after decimal to 4.
After an operation it can be achieved using toFixed() but while the user inputs the text is there any way out.
Please guide me.
Did you Try a REGEX?
The Regex would look like
/^[0-9]+.[0-9]{4}$/
where {4} says that Length after . would be 4
Define REGEX:
var regex1=/^[0-9]+.[0-9]{4}$/;var yourtextfromTextBox= textBox.text();yourtextfromTextBox.match(regex1);
Try this
You can use onkeyup="yourFunction" function to do so.
Just an idea: jsFiddle live demo
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Round to 4 Decimal Places</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$('#input').keypress(function(){
try {
val = $(this).val().split('.');
if(val[1] > 999) return false;
}catch(e){}
})
});
</script>
</head>
<body>
<p>Type a decimal number in the TextBox, limited to 4 decimals</p>
<input id="input" type="text" />
</body>
</html>
Of course, this works only for ., this could be improved by allow only numbers, and check for , too.
Here's a sample: http://jsfiddle.net/Regisc/5yber/
This use a regexp to validate the content of the input and make use of this function in order to take carret into account.
Tested under Chrome (dev)
function decimal_only(e) {
var charCode = (e.which) ? e.which : window.event.keyCode
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
if (charCode == 46) { //Now Check The Decimal only allow 1 decimal
if (e.value.indexOf('.') > -1) {
return false;
}
}
return true;
}
use this function on textbox event onkeypress.
like
onkeypress="return decimal_only(this)"
this will only allow 1 decimal and integer values to users

Categories