Here is a mind Boggling question i am having trouble with. I have a text field. This text field needs to accommodate U.S mobile numbers with 4 formats
XXX-XXX-XXXX
XXXXXXXXXX
X-XXX-XXX-XXXX
XXXXXXXXXXX
When the last character is entered it needs to do a check via ajax which i have no problem with.
I need to check if the textbox has the full mobile number in when typing, copying and pasting, deleting and every possible way to put numbers in that field.
i tried checking it with $("#id").keyup but that doesn't work when you paste something.
is there a way to check either via interval or something else if there is 10 or eleven characters in that field regardless of how it got there? [edit]
Here is a solution i found
var checkMsisdnInterval = window.setInterval( function () {
var msisdn = getdigits($("#newSessionMsisdn").val());
if(msisdn.length == 10 && !checked) {
doPreCheck(msisdn);
checked = true;
} else if(checked == true) {
//do something here
} else {
//do something else
}
}, (1000 * 60 * 0.1));
$("#newSessionMsisdn").keyup(function(){
checked = false;
});
function getdigits (s) {
return s.replace (/[^\d]/g, "");
}
it works like a bomb. Thanks for all the replies
Why not checking it in the ajax call, use the beforeSend method and check the length of the input field.
For your reference, http://api.jquery.com/jQuery.ajax/
Using jQuery, have you looked at the change event?
http://api.jquery.com/change/
Related
I know this seems a quite easy target. I have an input[type=text], and I want to detect the new added character(s) in it. The normal way is:
$selector.keypress(function(e) {
//do sth here
var newchar = String.fromCharCode(e.which);
});
But the above method not working properly for some browsers on android devices. Typing the android virtual keyboard will not fire the keypress.
Then I found the following method is better:
$selector.on('input', function(e){
//do sth here
});
It works fine for android devices, and also, it can detect cut/paste.
Now the question is, is there a way to know the new added character(s) to the input? Do I need to do the complicated string comparison during inputing each time, i.e. compare the previous string and the new string in the input box? I said it's complicated because you may not always type in char(s) at the end, you may insert some char(s) in the middle of the previous string. Think about this, the previous string in the input box is "abc", the new string after pasting is "abcxabc", how can we know the new pasted string is "abcx", or "xabc"?
The method from keypress is quite simple:
String.fromCharCode(e.which);
So, is there similar way to do this by the on('input') method?
After reading Yeldar Kurmangaliyev's answer, I dived into this issue for a while, and find this is really more complicated than my previous expectation. The key point here is that there's a way to get the cursor position by calling: selectionEnd.
As Yeldar Kurmangaliyev mentioned, his answer can't cover the situation:
it is not working is when you select text and paste another text with
replacing the original one.
Based on his answer, I modified the getInputedString function as following:
function getInputedString(prev, curr, selEnd) {
if (selEnd === 0) {
return "";
}
//note: substr(start,length) and substring(start,end) are different
var preLen = prev.length;
var curLen = curr.length;
var index = (preLen > selEnd) ? selEnd : preLen;
var subStrPrev;
var subStrCurr;
for(i=index; i > 0; i--){
subStrPrev = prev.substr(0, i);
subStrCurr = curr.substr(0, i);
if (subStrCurr === subStrPrev) {
var subInterval = selEnd - i;
var interval = curLen - preLen;
if (interval>subInterval) {
return curr.substring(i, selEnd+(interval-subInterval));
}
else{
return curr.substring(i, selEnd);
}
}
}
return curr.substring(0, selEnd);
}
The code is quite self explanation. The core idea is, no matter what character(s) were added(type or paste), the new content MUST be ended at the cursor position.
There's also one issue for my code, e.g. when the prev is abcabc|, you select them all, and paste abc, the return value from my code will be "". Actually, I think it's reasonable, because for my scenario, I think this is just the same with delete the abc from previous abcabc|.
Also, I changed the on('input') event to on('keyup'), the reason is, for some android browsers, the this.selectionEnd will not work in a same way, e.g., the previous text is abc|, now I paste de and the current string will be abcde|, but depending on different browsers, the this.selectionEnd inside on('input') may be 3, or 5. i.e. some browsers will report the cursor position before adding the input, some will report the cursor position after adding the input.
Eventually, I found on('keyup') worked in the same way for all the browsers I tested.
The whole demo is as following:
DEMO ON JSFIDDLE
Working on the cross-browser compatibility is always difficult, especially when you need to consider the touch screen ones. Hope this can help someone, and have fun.
Important notes:
when a user types in a character, the cursor stands after it
when a user pastes the text, the cursor is also located after the pasted text
Assuming this, we can try to suggest the inputed \ pasted string.
For example, when we have a string abc and it becomes abcx|abc (| is a cursor) - we know that actually he pasted "abcx", but not "xabc".
How do this algorithmically? Lets assume that we have the previous input abc and the current input: abcx|abc (cursor is after x).
The new one is of length 7, while the previous one is of length 4. It means that a user inputed 4 characters. Just return these four characters :)
The only case when it is not working is when you select text and paste another text with replacing the original one. I am sure you will come up with a solution for it yoruself :)
Here is the working snippet:
function getInputedString(prev, curr, selEnd) {
if (prev.length > curr.length) {
console.log("User has removed \ cut character(s)");
return "";
}
var lengthOfPasted = curr.length - prev.length;
if (curr.substr(0, selEnd - lengthOfPasted) + curr.substr(selEnd) === prev)
{
return curr.substr(selEnd - lengthOfPasted, lengthOfPasted);
} else {
console.log("The user has replaced a selection :(");
return "n\\a";
}
}
var prevText = "";
$("input").on('input', function() {
var lastInput = getInputedString(prevText, this.value, this.selectionEnd);
prevText = this.value;
$("#result").text("Last input: " + lastInput);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<div id="result">Start inputing...</div>
I have a webpage that has a textbox.
When the user enters information into it, it makes a AJAX call to see if the entry is valid, if not it disables a button.
They can also add up to 10 textboxes which is done via jQuery Templates. At the moment each textbox has a class of serial and when a serial textbox is blurred it does this check.
If they enter a invalid serial it will disable the button but if they add a new textbox and that is valid the button is now enabled which is wrong as there is one still invalid.
The only way I can think to do this is to add a 1 or 0 to an array for each textbox and once all elements in the array are 1 then enable the button. Is that a good approach, if not please explain a better one. If it is a good approach how do I check all values in a javascript array are the same?
Thanks
This sounds like a good approach. You can check for equal elements in a javascript array using this simple javascript function. You may paste this to a firebug console to check its functionality.
// check if all elements of my_array are equal, my_array needs to be an array
function check_for_equal_array_elements(my_array){
if (my_array.length == 1 || my_array.length == 0) {
return true;
}
for (i=0;i<my_array.length;i++){
if (i > 0 && my_array[i] != my_array[i-1]) {
return false;
}
}
return true;
}
//Example:
var my_array = [];
my_array.push(5);
my_array.push(5);
// will alert "true"
alert("all elements equal? "+check_for_equal_array_elements(my_array));
my_array.push(6);
// will alert "false"
alert("all elements equal? "+check_for_equal_array_elements(my_array));
I will assume you have a isValid(str) function that returns a boolean.
Since you're using jQuery, you can take advantage of jQuery's filter() function to easily check if any inputs are invalid whenever an input blurs:
$('.serial').live('blur', function () {
// Get an array of all invalid inputs
var invalids = $('.serial').filter(function () {
return !isValid($(this).val());
});
// Does the array contain anything?
$('#button').prop('disabled', invalids.length);
});
Demo: http://jsfiddle.net/3RNV6/
Similar concept, but for use with AJAX:
$('.serial').live('blur', function () {
var me = this;
$.ajax({
// ajax config
success: function (data) {
if (data === 'Y') $(me).addClass('valid');
// Get an array of all invalid inputs
var invalids = $('.serial').filter(function () {
return !$(this).hasClass('valid');
});
// Enable if none invalid
if (invalids.length === 0) $('#button').prop('disabled', false);
}
});
});
$('.serial').live('keypress', function () {
$('#button').prop('disabled', true);
$(this).removeClass('valid');
});
First of if you dynamically create n textboxes you should use live() or delegate() methods of jQuery to inform of new DOM elements added.
Secondly your approach is just fine but instead of an array you can set param of inputs with wrong text and then disable button if there are any elements with wrong text. I think it will be faster than looping though all textboxes all over.
I would use validation to achieve this.
http://docs.jquery.com/Plugins/Validation#Demos
If you can validate client-side great - either use one of the existing jQuery validation functions shown in the link above, or write your own.
If you must validate server side via ajax, then you could build this into a custom validation routine.
Then in the call that shows/hides the button - make a call to $('#formid).validate() - returns false if any validation fails.
I have a column in a jqgrid defined as number. The user is forced to enter a number like 6.5 with the comma delimeter being a point.
This grid is also used by german speaking people who are used to insert numbers like this 6,5 using the comma as the delimeter.
This people are getting upset when they are not allowed to insert 6,5 instead of 6.5 :D
To make their (and in the end) my life more convenient I'm looking for a way to convert automatically the 6,5 to 6.5 . This should be done on the client side, since I want to rely on jqgrid num checking.
Thus I should check (and maybe transform) the number before jqgrid is checking it.
Is this possible?
--edit--
None of these functions are called, except the first one.
Any idea why this could be the reason?
afterInsertRow:function (rowid, aData){
alert('fire');
},
beforeSaveCell : function(rowid,celname,value,iRow,iCol){
alert('no fire');
return "new value";
},
beforeSubmitCell : function(rowid,celname,value,iRow,iCol){
alert('no fire2');
return "new value";
},
beforeEditCell : function(rowid,celname,value,iRow,iCol){
alert('no fire3');
return "new value";
},
--edit2--
This is the code I'm using for inline editing.
onSelectRow: function(row_id){
if(row_id != null) {
var date_str = jQuery('#grid').getCell(row_id, 'date_str');
//var sum = jQuery('#grid').getCell(row_id, 'sum');
var description = jQuery('#grid').getCell(row_id, 'description');
if(date_str != "Total"){
if(row_id !== last_selected_row) {
if(row_id == -99){
//thats the first click of the user after initial load of the grid
jQuery('#grid').jqGrid('saveRow',row_id)
.editRow(row_id, true,true,reload);
last_selected_row = row_id;
}
else{
//after user jumps from one cell to another using the mouse
jQuery('#grid').jqGrid('saveRow',last_selected_row,reload);
jQuery('#grid').jqGrid('restoreRow',last_selected_row);
last_selected_row = row_id;
}
} else {
jQuery('#grid').jqGrid('saveRow',row_id)
.editRow(row_id, true,true,reload);
last_selected_row=row_id;
}
}
}
},
If you use cell editing, you can try to write your beforeSaveCell event handler which do the text replacement which you need.
UPDATED: I don't know which information can be placed in the cell, but probably the usage of Masked Input Plugin (see old answer here) or just 'keypress' filtering (see another answer) could improve user experience.
Basically what I'm trying to accomplish is I want to check multiple dynamic forms on a page. Then if All textareas are empty OR have the default value, perform a function. Otherwise if any of them are not empty without the default value, cancel the function.
I know how to get the total count of the divs/forms or go through each one, sorta. Just can't put it together. I want it to check them all first before doing anything.
if (jQuery("div[id^='statuscontainer_']:last").length == 0){
}
I don't think this would get quite what I need to do. Or either I not sure how to form it into the function I need.
Thanks
You could take this approach
Give each input a similar class, in my example, inputBox
Create a script to add the total of each inputBox length. If that total == 0, take whatever approach, otherwise continue with the script
$('#show').click(function() {
var total = 0;
$('.inputBox').each(function() {
total += this.value.length;
});
if (total == 0) {
// Do whatever if it's empty
} else {
// Do whatever if its not
}
});
???
Profit
Try it
http://jsfiddle.net/kLnHC/
Edit
Though, in your application, you'd probably want to change the event that triggers it to $('#yourForm').submit(function() { rather than a click function.
use
total += $(this).val();
instead of
total += this.value.length;
will check if there is only a " " and/or a \n in your textarea.
Is there a canonical solution for limiting the number of characters that someone can enter into a textarea?
I have server side validation of course, but would like to improve the user experience by adding client side code to simulate the experience you get with maxlength on an input[type="text"] so that users never see the "your input is too long" error message if they have javascript enabled.
My non-technical $0.02
Do what SO does in their comment fields, provide feedback as to what the user's character length remaining is and allow them to go past the maximum, but not submit > 300 characters. Here's why:
Allowing users to go over the maximum number of characters allows them to complete an idea. They can then go back and edit that idea. There's nothing worse than being at the end of a sentence and being unable to complete it.
Providing feedback to character length keeps people from having to guess what their length is at.
I would do it this way:
$ ('#textarea-id').bind (
'change input keyup keydown keypress mouseup mousedown cut copy paste',
function () { return ($(this).val().length <= maxlength) }
)
So many bingings just to be completely sure :-)
This will do it...
function textareaMaxLen(event) {
var max = parseInt(this.attr("maxlength"), 10);
if(this.value.length >= max)
this.value = this.value.substr(0, max);
}
$("textarea").keyup(textareaMaxLen).blur(textareaMaxLen);
Attach an onchange event to the textarea. There you can check if the value's larger than the appropriate or not. Example with jQuery:
$("#textarea-id").change(function (){
var text = $(this).val();
if( text.length > MAX ){
return false;
}
});
I've done it like this in the past:
<textarea onkeyup="checkLength(this);"></textarea>
Then implement this function:
function checkLength(control) {
if (control.value.length > 5) {
control.value = control.value.substr(0, 5);
}
}
It's a pretty basic example fixing the length to 5, but hopefully gives you the idea!