I have a javascript code that allows me to jump automatically from a form field to the next.
It works using fixed lenght fields.
Example: field TIME can be only 4 numbers, so when the user enters the 4th number, the script focuses on the next field.
It works fine, but I would like to add a feature.
I want to use it in a variable lenght field. My field is composed of LASTNAME (space) First letter of FIRSTNAME.
Example: John Doe will be typed as "Doe J"
The only trick I can think of is to make the field shift when space is pressed + another character is entered. This is the only repeating pattern that would allow the function to be executed with any combination of variable lenght lastnames.
So, any idea how to implement it? I am a beginner in js! Here is the original code:
<SCRIPT TYPE="text/javascript">
<!--
var downStrokeField;
function autojump(fieldName,nextFieldName,fakemaxlength)
{
var myForm=document.forms[document.forms.length - 1];
var myField=myForm.elements[fieldName];
myField.nextField=myForm.elements[nextFieldName];
if (myField.maxlength == null)
myField.maxlength=fakemaxlength;
myField.onkeydown=autojump_keyDown;
myField.onkeyup=autojump_keyUp;
}
function autojump_keyDown()
{
this.beforeLength=this.value.length;
downStrokeField=this;
}
function autojump_keyUp()
{
if (
(this == downStrokeField) &&
(this.value.length > this.beforeLength) &&
(this.value.length >= this.maxlength)
)
this.nextField.focus();
downStrokeField=null;
}
//-->
</SCRIPT>
Rather than detecting a space and then a letter, take a look at the string.match() method.
You can do something like name.match(/^[A-Z][a-z]* [A-Z]$/) on keypress to determine if name contains a capital letter followed by any number of lower-case letters followed by a space and another capital letter.
However, be aware that you may run into issues with your criteria, such as someone who has a title like 'Jr', or last names that have spaces, like "Da Silva" or apostrophes, like "O'Malley". Accommodating the many special cases in peoples names can be tricky.
Related
It has a logic, where the user types in the input and at the same time the mask occurs, in this case, two conditions can happen:
If he is entering a monetary value, only the numbers entered will be accepted and he will fill in for me the "." or "," according to the decimal place (this code is not here), example:
"BRL 1,000.00"
If he is typing a percentage value, the same thing happens as in case (1), but he inserts the following, for example:
"1,000.00%"
Cool.. This works, however, now I'm trying to rack his brain for him to do this, if he wants a negative number, he should type the minus sign (-), regardless of where the selection pointer is in the input , this sign must be to the left of the first number, for example:
"BRL -1,000.00"
"-1,000.00%"
I'm trying to do this with regex, I managed to release the minus sign (-), however, I can't put it to the left of the last number or block it to be typed only once by the user, so what happens is:
"BRL 0.0-"
"BRL -.11"
Code:
value = value
.toString()
.replace(/[^0-9-]/g, '')
.replace(/^0+(?!$)/, '')
.substring(0, maxNumber);
I really can't understand what you want (after reading your text twice), but on my side it works as I guess you would expect.
Just run the snippet
const convert = (value) => value
.toString()
.replace(/[^0-9-]/g, '')
.replace(/^0+(?!$)/, '')
.substring(0, 50);
console.log(convert("BRL -1,000.00"))
console.log(convert("-1,000.00%"))
Do you want to remove the minus sign, is that?
I have one requirement in form.My form has textbox field "DEA License number".textbox must allow user to enter alphabet only for first two characters and numbers only after two characters.I want to achieve this functionality using javascript.Note:-I dont want validation but avoid user inputting
Have you tried using regex?
Take a look at this post which has a very similar goal: RegEx pattern any two letters followed by six numbers
Try use some of masked textbox.
For example:
https://css-tricks.com/input-masking/
http://digitalbush.com/projects/masked-input-plugin/
You can easily test this with a regex:
function isValid(str) {
return /^[a-zA-Z]{2}\d+$/.test(str);
}
I am not quite sure what you mean by "I dont want validation but avoid user inputting." If you mean that you don't want the user to be able to type an invalid character, this could theoretically be done with an input event handler:
var oldValue = "";
document.getElementById("test").addEventListener("input", function(e) {
var value = e.target.value;
if ((value.length <= 2 && /^[a-zA-Z]*$/.test(value)) || value.length > 2 && /^[a-zA-Z]{2}\d+$/.test(value)) oldValue = value
else e.target.value = oldValue;
})
However, you'd still need to validate it when it's submitted since the user could've entered an incomplete value.
I have an input field in which I want to allow only number and 1 comma. How could I make it accept only single comma?
$("#my-field").on("keyup", checkKey);
function checkKey() {
this.value = this.value.replace(/[^0-9,]/g, "");
}
You could do it like this:
function checkKey() {
var clean = this.value.replace(/[^0-9,]/g, "")
.replace(/(,.*?),(.*,)?/, "$1");
// don't move cursor to end if no change
if (clean !== this.value) this.value = clean;
}
// demo
document.querySelector('input').oninput = checkKey;
<input>
This will remove all repeated commas, and everything between them. That is not an issue, since you press one key at a time.
Remarks
This blocking way of input validation is user unfriendly. It is better to colour things, or put messages, than to make the keyboard disfunctional.
Consider using the <input type="number"> element, which has number validation built in.
The input event is often more useful for checking for changes in the input than keyup, since changes can also be made via mouse actions and the context menu.
If you want to allow dot instead of comma, then change every , with \. in the regular expressions, as . has a special meaning in regular expressions, and must be escaped to be taken as a literal character.
Trying to figure out how to detect if two words have been typed in an inputbox with jquery or javascript.
I am not sure how to do this, I can do a character limit but cant quite figure out if two words have been typed.
This is just a question, I do not have any code to display as I do not know where to begin
Why not counting spaces ?
function howManyWordsInInput(id)
{
var text = document.getElementById(id).value;
text = text.split(" ");
return text.length;
}
Usage for 2 words : if (howManyWordsInInput("YourInputID") == 2) {...}
EDIT
For those who prefer single lined way :
if (document.getElementById("YourInputID").value.split(" ").length == 2) {...}
if$('input').val().split(/\s/).length===2){
// 2 words have been typed
}
And bind this to the keyup event.
There are other ways too. Many
note that this dosent account for the user typing non word characters or numbers.
Just use split to see how many words You have: http://fiddle.jshell.net/m4y9tscx/
I currently use the script below to take any form of information and pull out the numbers to make a phone number with dashes in it. However, if I accidently click in the field, it puts two dashes in there even though nothing was pasted or typed. Does JS have a way to say ONLY if something is pasted then add dashes? The reason it's a pain is I have 2 search fields, and if I want to use one, the other has to be blank. So if there are 2 dashes in it, I have to delete them out and hit enter in the same field or it will add them again.
I appreciate any help you might have.
<SCRIPT LANGUAGE="JavaScript">
function addDashes(f)
{
f.value = f.value.replace(/\D/g, '');
f.value = f.value.slice(0,3)+"-"+f.value.slice(3,6)+"-"+f.value.slice(6,15);
}
</SCRIPT>
I'd go with something like this:
function addDashes(f) {
f.value = f.value.replace(/(\d{3})-?(\d{3})-?(\d{9})/, '$1-$2-$3');
}
It'll only do something if you have 15 digits there (possibly with dashes already in place).
I'm not quite sure if this is what you need, but this just checks to see if there is actually something there before formatting it:
function addDashes(f)
{
var oldVal = f.value.replace(/\D/g, '');
if (oldVal.length) {
f.value = oldVal.slice(0,3) + "-" + oldVal.slice(3,6) + "-" + oldVal.slice(6,15);
}
}
EDIT:
Based on your comment, I thought it might be helpful to bring up validation. I'm not sure if you are doing anything on the server-side to make sure it is a valid phone number, but it might be helpful to do a little validation so taht you don't add dashes if the user has just typed some spaces.
First, I would remove the non-numeric values before you check the length. I've updated the code above to do that.
Next, I would check against some length. Maybe you want to only add dashes if the number is at least 9 digits long. You can decide that length taht you watn to check against. In that case, you would add:
if (oldVal.length >= 9) { ...
It all depends on if/how you are validating this field.