How check input maxlength < 1GB? - javascript

I have 1 TextBox input data in PHP.
I want check limit input.
How check input maxlength < 1GB?

Why do you need to know the byte size of the input?
Regardless, to calculate the byte size of a string in Javascript you can use:
function stringSize(s) {
return encodeURI(s).split(/%..|./).length - 1;
}
This assumes that you're using UTF-8. Not sure if this would work with other encodings.

This can be counting the lines of characters ..The code will be
var value = document.getElementById('titleeee').value;
if (value.length < 3) { //replace with your character length
//the rest of ur code
}
Please see here for more info

1GB = 1024mb = 1048576kb = 1073741824 characters

Related

Calculate remaining character for storing in mysql

I have a column named DESC in mysql database. I set it to Varchar(255) latin-1.
Before storing data in mysql database i use
filter_var(mysqli_real_escape_string($value),FILTER_SANITIZE_SPECIAL_CHARS)
My question is how do I implement jquery character counting and maxlength restriction and be able to retrieve & display all the data from the textarea?
My Pseudo code
// Set MaxLength to 255
max= 255;
// Get length of textarea
$(this).val().length
// Count how many &, <,>,",' then multiply by 4 (as each of these characters will convert into &#??? which add extra 4 characters into database) and Newline (add extra 1 character) in the textarea.
CountSpecial($(this).val()); //bug #1
// calculate remaining characters.
Remaining = max - $(this).val().length -CountSpecial($(this).val());
//Limit the data to varchar(255) to avoid data truncation.
If (Remaining <0) {
//truncate to fix varchar(255)
$(this).val($this.val().substring(0,$this.val().length+Remaining); //bug#2
//recalculate Remaining.
Remaining = max - $(this).val().length -CountSpecial($(this).val());
}
Bug #1, I dont know how to accurately calculate the extra characters will be stored into database
Bug #2 I tried
$(this).val($this.val().substring(0,$this.val().length-1);
but it doesn't behave the way I want, as I use keyup function, if the user keeps holding a key, this calculation will not work.
$(this).val($this.val().substring(0,$this.val().length+Remaining);
fixed the problem when the user keeps holding a key, but product another problem. If the user key in something like "&" it will remove the last 4 characters from textarea not 1.
Here is my original code
http://jsfiddle.net/seth1111/orLh4v4w/5/
How do i fix these bugs?
Thanks.
You can create a jQuery Remaining character counter by this simple code.
$(function() {
$(document).on('keyup paste', '#address', function (e) {
var value = $(this).val();
var limit = 10; // Max character you want to allow
var length = value.length;
var remaining = parseInt(limit - length);
$('#result').html(remaining+' Character(s) Remaining');
if(length < limit)
{
$('#result').show();
$('#error').hide().removeClass('error').html('');
}
else if(length > limit)
{
$('#result').hide();
this.value = this.value.substring(0, limit);
$('#error').show().addClass('error').html('Maximum ' + limit + ' Characters are allowed!');
}
});
});
HTML part is
Enter Address: <textarea cols="21" rows="5" name="address" id="address"></textarea>
<p id="result"></p>
<p id="error" style="display: none;"></p>
CSS for error class
.error {
color:#f00 !important;
}
Just this simple code will done your task.
If you still need help feel free to comment.
You can simply check special characters and new lines with the String.match function in javascript.
realLength = $(this).val().length;
specialCarLength = $(this).val().match(/[&"',<>]/g);
specialCarLength = specialCarLength === null ? 0 : specialCarLength.length * 4;
NewLineCarLength = $(this).val().match(/\n/g);
NewLineCarLength = NewLineCarLength === null ? 0 : NewLineCarLength.length * 2;
NormalCarLength = realLength-specialCarLength/4-NewLineCarLength/2;
check my jsFiddle, it demonstrate how you can match special characters and new lines : jsFiddle

if condition not working properly in html js

in if condition i get value max 10 min 9 when compare its true and exe how its possible
for(var r=1;r<n;r++)
{ c=0;
max=table.rows[r].cells[c].innerHTML;
if (max<minimum)
{
alert("if max"+max+" minimum"+minimum );
minimum = max;
alert(minimum);
location = r+1;
}
else
{
minimum=minimum;
alert("else minimum"+minimum);``
}
}
}
Thank you
give me solutions to find correct answer or else i need code for highlight in table cell which cells are all have minimum value in a table
max=table.rows[r].cells[c].innerHTML;
InnerHTML is a string. In order to do comparison you need to convert it to a number with parseInt or parseFloat
max = parseInt(table.rows[r].cells[c].innerHTML);
You have to use parseInt() method because the verification will be done on a string:
max=parseInt(table.rows[r].cells[c].innerHTML);

How to append an extra 'Zero' after decimal in Javascript

Hye,
Iam new to javascript working with one textbox validation for decimal numbers . Example format should be 66,00 .but if user type 66,0 and dont type two zero after comma then after leaving text box it should automatically append to it .so that it would be correct format of it . How can i get this .How can i append ?? here is my code snippet.
function check2(sender){
var error = false;
var regex = '^[0-9][0-9],[0-9][0-9]$';
var v = $(sender).val();
var index = v.indexOf(',');
var characterToTest = v.charAt(index + 1);
var nextCharAfterComma = v.charAt(index + 2);
if (characterToTest == '0') {
//here need to add
}
}
Use .toFixed(2)
Read this article: http://www.javascriptkit.com/javatutors/formatnumber.shtml
|EDIT| This will also fix the issue if a user types in too many decimals. Better to do it this way, rather than having a if to check each digit after the comma.
.toFixed() converts a number to string and if you try to convert it to a float like 10.00
then it is impossible.
Example-
10.toFixed(2) // "10.00" string
parseFloat("10.00") // 10
Number("10.00") // 10

How do I get the unicode/hex representation of a symbol out of the HTML using JavaScript/jQuery?

Say I have an element like this...
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mo class="symbol">α</mo>
</math>
Is there a way to get the unicode/hex value of alpha α, &#x03B1, using JavaScript/jQuery? Something like...
$('.symbol').text().unicode(); // I know unicode() doesn't exist
$('.symbol').text().hex(); // I know hex() doesn't exist
I need &#x03B1 instead of α and it seems like anytime I insert &#x03B1 into the DOM and try to retrieve it right away, it gets rendered and I can't get &#x03B1 back; I just get α.
Using mostly plain JavaScript, you should be able to do:
function entityForSymbolInContainer(selector) {
var code = $(selector).text().charCodeAt(0);
var codeHex = code.toString(16).toUpperCase();
while (codeHex.length < 4) {
codeHex = "0" + codeHex;
}
return "&#x" + codeHex + ";";
}
Here's an example: http://jsfiddle.net/btWur/
charCodeAt will get you the decimal value of the string:
"α".charCodeAt(0); //returns 945
0x03b1 === 945; //returns true
toString will then get the hex string
(945).toString(16); // returns "3b1"
(Confirmed to work in IE9 and Chrome)
If you would try to convert Unicode character out of BMP (basic multilingual plane) in ways above - you are up for a nasty surprise. Characters out of BMP are encoded as multiple UTF16 values for example:
"🔒".length = 2 (one part for shackle one part for lock base :) )
so "🔒".charCodeAt(0) will give you 55357 which is only 'half' of number while "🔒".charCodeAt(1) will give you 56594 which is the other half.
To get char codes for those values you might wanna use use following string extension function
String.prototype.charCodeUTF32 = function(){
return ((((this.charCodeAt(0)-0xD800)*0x400) + (this.charCodeAt(1)-0xDC00) + 0x10000));
};
you can also use it like this
"&#x"+("🔒".charCodeUTF32()).toString(16)+";"
to get html hex codes.
Hope this saves you some time.
for example in case you need to convert this hex code to unicode
e68891e4bda0e4bb96
pick two character time by time ,
if the dec ascii code is over 127 , add a % before
return url decode string
function hex2a(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2){
var dec = parseInt(hex.substr(i, 2), 16);
character = String.fromCharCode(dec);
if (dec > 127)
character = "%"+hex.substr(i,2);
str += character;
}
return decodeURI(str);
}

converting numbers into alphabets

I want to convert numbers into alpha characters using JavaScript. For example, 01=n, 02=i 03=n, 04=a, etc.
When someone enters the numbers:01020304 in the form he will get the response: nina. Whatever the user enters gets replaced with the equivalent characters including spaces.
Update
Thank you all for quick response. I have found this code in one site. It converts alpha characters into numbers, but code for converting numbers into alpha characters isn't working. Here is the code for converting alpha characters into numbers:
var i,j;
var getc;
var len;
var num, alpha;
num=new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17",
"18","19","20","21","22","23","24","25","26","00","##","$$");
alpha=new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","
v","w","x","y","z"," ",".",",");
function encode() {
len=document.f1.ta1.value.length;
document.f1.ta2.value="";
for(i=0;i<len;i++) {
getc=document.f1.ta1.value.charAt(i);
getc=getc.toLowerCase();
for(j=0;j<alpha.length;j++) {
if(alpha[j]==getc) {
document.f1.ta2.value+=num[j];
}
}
}
}
Can anyone show me how to convert this to do the opposite character conversion?
I agree with Skrilldrick, you should learn how to do this yourself, but I couldn't help myself: http://jsfiddle.net/dQkxw/
HTML
<html>
<body>
<input type="text" id="code">
<button onclick="decode($('#code').val())">
Decode
</button>
</body>
</html>
JavaScript
window.decode = function(numbers) {
if (numbers.length % 2 != 0)
{
alert("invalid code!");
return;
}
var result = "";
for (var i = 0; i < numbers.length; i+=2) {
var number = Number(numbers.substring(i, i+2));
if (number < 1 || number > 26)
{
alert("invalid number: "+number);
return;
}
result += String.fromCharCode(96+number);
}
alert(result);
}
A good way to do this easily, and so it is a scalable solution would be to have a multi dimensional array that maps each char to it's corresponding char. You can have multiple dimensions for each conversion and pick between them.
var myCharArray=new Array(4)
for (i=0; i < 4; i++)
myCharArray[i]=new Array(2)
myCharArray[0][0]="a"
myCharArray[0][1]="1"
myCharArray[1][0]="b"
myCharArray[1][1]="2"
myCharArray[2][0]="c"
myCharArray[2][1]="3"
myCharArray[3][0]="d"
myCharArray[3][1]="4"
Then, upon conversion, loop every single character in your string to be encoded, and search for it in the array. If it is found, switch it with the encoded value. This should be reasonably easy to do.
The method you described seems to be a simple derivative off a Caesar cipher. Also remember because the script is client side, it will be incredible easy to decode, so make sure it's not for anything important!

Categories

Resources