Check value of starting characters of an input Jquery - javascript

I have a text field that can accept input of any kind. Based on the input, I need to make some fields hidden and others unhidden. However I want to put another condition within the first condition to check for the first 3 characters of the input value
Here is my code for the first condition:
$("#accountcodes").live("focusout",function(){
var code = $(this).val();
if(code>30000){
alert("T1 to T4 codes needed");
$(this).parents("tr").find('#T1').removeAttr('hidden','hidden');
$(this).parents("tr").find('#T2').removeAttr('hidden','hidden');
$(this).parents("tr").find('#T3').removeAttr('hidden','hidden');
$(this).parents("tr").find('#T4').removeAttr('hidden','hidden');
}
})
I want to put another condition within the if statement(if within an if) to check if the first 3 characters start with 310 or 311 then do something else e.g if a user inputs 31102, then another field is unhidden e.t.c I am not sure how to do that in Jquery. should I use regex? do I take the value of input and cut out the first three characters and check it?
Any help will be appreciated

So you just want to know about substr()? code.substr(0,3) would get you the first three characters.
Alternatively, you may want to use a regex to find 310 and 311 more easily, in which case you want code.match(/^31[01]/) instead.

var code = $(this).val();
var firstThreeChars = code.substr(0, 3);

It seems your values will always be numeric. You don't need much jquery to achieve this.
$("#accountcodes").live("focusout",function(){
var code = $(this).val();
if(code > 30000){
//Note: For any non-numeric value, this condition will always be false
alert("T1 to T4 codes needed");
var p = $(this).parents("tr");
p.find('#T1, #T2, #T3, #T4').removeAttr('hidden');
if ((code - 31000) > 102){ // Your other condition check can look like this
// unhide other fields
}
}
});
Hope this will help. Happy Coding.

Your Question have your answer you can do it many ways
Sub-string the value and check the value.
2.go for contains key word of jquery [ api.jquery.com/contains-selector/][1]

Related

I am facing problems in Conditional Statements With input Fields Of bootstrap

Here Is My Logic:
It Didn't Work :(
function ShowDropMenu(){
var numfiled = document.getElementById('numpeple'); //I Want Whenever Someone Enters a Number In Input Field It Should Display The Dropdown Menu?
if (numfiled < 0 ) {
document.getElementById('dropmock').style.display="block"
}
else{
alert('Please Add Atleast 1 Or More People to go ahead)
}
}
I Don't Know What's Went Wrong?
numfiled is an Element, so comparing it with a number won't do anything meaningful.
You want the value of the Element. Additionally, since the value is always a string, you want to parse it as a number. For example:
var numfiled = parseInt(document.getElementById('numpeple').value);
Note: This assumes #numpeple refers to an <input> element, inferred from the comment in the code: "Someone Enters a Number In Input Field"

Split into separate fields an input text

I'm having a trouble when i try to force the user to enter data to a text input. I need to do something like the IP Address input in Windows.
I want to split the text input by dashes having something like this
10 - 10 - 10 - 10
Is there any way to do this ?
http://jsfiddle.net/87dug9oa/1/
function check(text) {
var result = [];
text = text.replace(/[^\d]/g,"");
while (text.length >= 3) {
result.push(text.substring(0, 3));
text = text.substring(3);
}
if(text.length > 0) result.push(text);
$("#ip").val(result.join("-"));
}
$("#ip").on("keyup", function() {
check($(this).val());
});
This creates a function, which adds dashes once 3 characters has been added (and the fourth is written).
Now, this does do what you want, but you need to add some additional stuff, such as checking for length and making the remove part work (because when you press any key, it will change the input's value, which will make the caret move to the last character).
Oh I almost forgot. This can be changed to the length of your choice, of course. Just change the "3" to be something else.

Instantaneous word counter

I'd like to add a word counter to a large text box (think blog editor) that increments with every word that I enter. (i.e. after I type "Hello" it would increment from 0 to 1 and after I write "world" after that it would increment to 2). I'd like to do this within a webpage but I'm happy to consider other options.
I have no idea how to do this. I think there would be a way to do this with Javascript but I know next to nothing about Javascript.
Could someone point me in the direction of figuring out how to do this?
I think a good idea to implement this is to check inputed letter (for example, onKeyUp event). If previous character was non-space and current is not a letter, then increment count. Detail: if previous character is non-letter, increasing should not be done (double spaces e t.c.)
When user is pressing Backspace or Delete key, similar check should be done. Key codes you can find here
The difficult thing with this counting 'on the fly' is to check 'non-key' input. For example, user can use Ctrl+Insert combination to copy data from clipboard, or select text in the textbox and then press Del key. That should be handled separately if there is real need to avoid whole value processing (which could be really huge)
Use this functionality on the onkeyup event for the textbox.
document.getElementById("inputbox").addEventListener("keyup",function(e){
// Get the inputs text.
var inputVal = e.srcElement.value;
var counter = 0;
// Check input isn't empty.
if(inputVal){
// Count individual letters.
var wordlist = inputVal.split(" ");
for(var i=0;i<wordlist.length;i++)
if(wordlist[i])
counter = counter+1;
}
// Then set your counter element.
document.getElementById("counter").value = counter;
});

Jquery How to bind the last digit

i have a table like below which each content is input pop-up
on square i spot you see on last digit have 1,2,8,9 format.
in my html the content of table is value of Posisi
Nomor Rak
<br><input type="text" id="posisi" readonly/></br>
that automaticly i pick using
<td class="data206"><div align="center"><input class="data206" type="button" onclick="popup_window_show('#sample', { pos : 'tag-right-down', parent : this});setvalue(this.value);" value="A1.7.8" /></div></td>
for an example.
so my pop-up like this
my purpose to bind the last digit and then i can manipulate line value. So for an idea script will like below
$(document).ready(function(){
var posisi = $("#posisi").val();
if (posisi== like "1" or like "2" or like "8" or like "9" ){
$("#line").val("9")
}
});
My problem: I don't know how to bind the last digit in jquery..to make conditonal that $("#posisi") last digit value between 1 or 2 or 8 or 9. if this conditional true i can add value in $("#line").val("whatever i want")
need help with an example will be appreciate
Your var posisi will contain a string like "A1.7.8" and you can easily get the last character of string. This may help
How can I get last characters of a string using JavaScript
There are a few more tips which may help.
It seems that you do not want to wrap the code in document.ready. I think you want to get the last digit value on every click. so get the value in "popup_window_show" function or function you are using to show popup.
Moreover if you want to make calculation on that number i.e 1,2,8,9 then convert it into integer form first.
var posisi = $("#posisi").val();
var regex = /\.(\d+)\.?$/;
// Catches the last set of digits following a period.
// Allowing for an additional period at the end.
var matches = posisi.match(regex);
if (matches) {
var lastNumber = parseInt(matches[1], 10);
// Do something
$("#line").val(lastNumber);
}

Alphabetical Sorting with <input>

i would like to have a sorting routine for 5 words to sort alphabetical (From A-Z and Z-A), what I came up with (with help from some sites) was this. (excuses, I couldn't post a screen capture due not enough rep)
Script:
Function Sorter(){
var routine=["Banana","Milkshake","Work","bladiebla","Progress"];
routine.sort();
(routine.reverse();) if possible
var x=document.GetElementById("demo");
x.innerHTML=routine;}
this works, and is set in work by a button (not in this code). however what i want is that i can fill in the words myself on my site. i have used prompt tags and input tags but they dont seem to work (when i do it).
could anyone please help me out with this.
what i would like: i would like to be able to use prompt tags (or input tags) to put the words in the array and then sort the words alphabetically A-Z and backwards Z-A.
if someone would want to help me out with this then i would like that,
thanks in advance, Dim
I assume that you want to add the value entered in input box into an array and list the values in asc/desc order. I have created the jsFiddle it might be helpful.
$('input').bind('keyup', function(e) {
if(e.keyCode == 13 && $.trim(this.value)) {
routine.push(this.value);
Sorter();
this.value = '';
}
});
var routine=["Banana","Milkshake","Work","bladiebla","Progress"];
function Sorter() {
routine.sort();
var x = document.getElementById("demo");
x.innerHTML = routine.join('<br>');
}
Sorter();

Categories