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
Related
I have a string - project description ( as part of an object ) coming from a user form submission that is shown on a page of a report. If the line numbers exceed 24 I want to show the rest of the string on a new page. My initial idea was to cut it based on characters but this can't be done precisely as if line breaks are made when submitting the form, the characters can't be calculated as we donĀ“t know if the line break was made in the middle of a line or the end or wherever. I don't know what could be the solution?
How can I cut a string based on number of lines?
This is what I have done so far:
function countLines (el) {
let projectDetails = $rootScope.report.description;
var el = document.getElementById(el);
var divHeight = el.offsetHeight
var lines = divHeight / 17;
//console.log("Lines counted: " + lines);
if(lines > 24) {
$scope.secondDescriptionPage = true;
$scope.projectDetailsTextFirstPart = // this should be calculated
//$scope.projectDetailsTextSecondPart = // this should be calculated )
}
}
With the -webkit-line-clamp CSS property you can cut text by a certain number of lines. See MDN for details. It will not work in IE11 however.
how can I add leading zero to my column when my column's value is less than 10?
here is my code:
db.all("SELECT * FROM tablename WHERE x= ? ORDER BY MEMBER_CODE DESC LIMIT 1",[x], function(err,rows) {
if(rows.length == 0)
{
var mcode = 0;
}
else
{
var mcode = rows[0].MEMBER_CODE;
if (mcode < 10) {
mcode = "0"+mcode;
console.log(mcode);
}
} db.run("INSERT INTO f11 MEMBER_CODE VALUES $MEMBER_CODE",{ $MEMBER_CODE : +mcode+1});
From the code, MEMBER CODE increments every input. I tried that code but it fails. the output of console.log(mcode) which appends zero less than 10 is the one i want but it doesnt append zero anymore when the data will be processed on the database
console.log(mcode) outputs 01,02,03,04,05,06,...,10++
BUT
+mcode+1 outputs 1,2,3,4,5,6,...,10++
Please bear with my question and my explanation. Thanks
A list of isbn's are entered in the text area and javascript opens an amazon search for each one entered.
Isbn's can be between 10 or 13 digits.
Not all isbn searches start with an isbn, sometimes there can be information in front such as "isbn10:0195433831" so you cant count from the start.
For Example, this is a typical search:
0195433831 Good
0195433831 Poor
0195433831 Excellent
Question
Often times isbn's are entered in a different format breaking it, for example:
1) With spacing between numbers
978 0 132 76682 1 like new
978 0 495 38500 4 very good
2) With additional rating numbers added creating additional unnecessary
searches.
9781118624616 9/10 condition with minimal highlighting
9780415462020 10/10 condition, brand new
So I must find a way to have Javascript filter out these conditions.
Code is here:
//the input box.
var input = document.getElementById('numbers');
//adding an event listener for change on the input box
input.addEventListener('input', handler, false);
//function that runs when the change event is emitted
function handler () {
var items = input.value.replace(/\r?\n/g, ' ').split(' ');
length = items.length;
console.log('your collection', items);
for (var i = 0; i < length; i++) {
if ( items[i] && !isNaN(items[i]) ) {
console.log('opening page for isbn ', items[i])
openPage(items[i]);
}
}
}
//opens the tab for one isbn number
function openPage (isbn) {
var base = 'https://www.amazon.com/gp/search/ref=sr_adv_b/?search-alias=stripbooks&field-isbn='
window.open(base + isbn)
}
<p>... note, after paste you may need to click outside the text area or tab out to fire the change event.</p>
<textarea id=numbers placeholder="paste isbn numbers as csv here">
</textarea>
ISBNs should be 13 numbers if I remember right so can't you just remove the whitespace and match groups of 13?
var str = "978 111 862 4616 9/10 condition with minimal highlighting\n\n9780415462020 10/10 condition, brand new",
nums = str.replace(/\s/,"").match(/\d{13}/g);
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
I'd like to use jQuery to echo text as it is typed, into a text area below the input line, and as it does, put random pre-selected words on the end of that repeated data. Each time a new letter is typed as input, I'd like some other "random" word appended each of the 3 "repeats", kind of a jumble.
Input= "true blue"
TextArea =
true blue black<br/>
true blue car<br/>
true blue sock<br/>
If the input = "true blues"
TextArea =
true blues snake<br/>
true blues grass<br/>
true blues red<br/>
All new results because "s" was typed. (or as close to "new/random" as can be easily)
I'm going to have 200 words in an array, and with each key press I'd like the appended words to change (onkeyup?). If they can appear random or can be randomly picked from a larger list/file.
Input from the form is repeated for 3 lines in the text area, and 3 random words are also appended basically. Onkeypress echo input + random_word (x3).
Interesting challenge.
I've played with it for a couple of hours and have come up with a rudimentary, inelegant attempt - brute force.
FIDDLE
JS
var spacecount = 0;
var sumcodes = 0;
var wordarray = 0;
var randword = ['gould','horowitz','bach','scarlatti','handel','vivaldi','corelli','albinoni','allegri','pinnock',];
$(function(){
$(document).keyup(function(e){
var char1 = String.fromCharCode(e.keyCode);
$('#keycode').html(e.keyCode);
$('#charcode').html(char1);
$('#texthere').append(char1);
sumcodes = sumcodes + e.keyCode;
if (e.keyCode == 32)
{
spacecount=spacecount + 1;
$('#spacecount').html(spacecount);
}
if (spacecount > 2)
{
spacecount = 0;
wordarr = Math.floor(Math.random() * 10) + 1
$('#texthere').append(randword[wordarr] + ' ');
$('#sumcodes').html(sumcodes);
sumcodes = 0;
}
});
});
The random number generator is not that robust. I tried to relate it to the sum of the ASCII character codes, but was unsuccessful (I'll play a bit more).