How to create a validation on textbox? - javascript

I have 3 textboxes, I want to create a validation in textbox. For Example textbox1 value is 10, then textbox2.1stquarter value is 5, textbox3.2ndquarter should have atleast 5 or lower than 5. If the combine value of textbox2 and textbox3 is equal to 10 allow it but if the combine value of textbox2 and textbox3 is greater than 10 should not allow it. How to do it?
Just like this code. I want to get the value of textbox1so the two other textbox should have >10
function CompareValues() {
var textBox1=document.GetElementById('ID_OF_TextBox1');
var textBox2=document.GetElementById('ID_OF_TextBox2');
var textBox3=document.GetElementById('ID_OF_TextBox3');
if ( textBox1.value>textBox2.value + textBox3.value ) {
the value is greater than textbox1
}
}

Seriously you could have done it by yourself mate... Check if the below fiddle helps u... :)
var textBox1=document.getElementById('textbox1').value;
var textBox2=document.getElementById('textbox2').value;
var textBox3=document.getElementById('textbox3').value;
if((+textBox2 + +textBox3) >= 10){
$('#textbox1').val('');
$('#textbox2').val('');
$('#textbox3').val('');
return false;
}else{
alert('value ok');
}
http://jsfiddle.net/B5cKr/

HTML:
<input id="txt1" type="text" />
<input id="txt2" type="text" />
<input id="txt3" type="text" />
<DIV id="btnValid">CLICK</DIV>
JS:
$("#btnValid").on('click', function () {
var txt1 = $("#txt1").val();
var txt2 = $("#txt2").val();
var txt3 = $("#txt3").val();
var combine_2_3 = Number(txt2) + Number(txt3);
if (Number(txt1) > Number(txt2) || Number(txt1) > Number(txt3)) {
if (Number(txt1) > Number(combine_2_3)) {
alert("txt1 value is greater then combine value ");
}
else {
alert("combine value txt2, txt3 is greater then txt1");
$("#txt1").val("");
$("#txt2").val("");
$("#txt3").val("");
}
}
});
DEMO
Updated DEMO

Related

2 textbox that copies each other value while typing, but the other textbox has no comma

I'm currently working with 2 textboxes that copies each other values. But the thing is, my 1st textbox has an autocomma. How could I make my 2nd textbox ignore the comma?
For example. My first textbox value is 1,000 then my 2nd textbox
value should be 1000.
HTML
<input type="text" value="" id="textbox1"/>
<input type="text" value="" id="textbox2"/>
Script
//this function is for my autocomma
function updateTextView(_obj){
var num = getNumber(_obj.val());
if(num==0){
_obj.val('');
}else{
_obj.val(num.toLocaleString());
}
}
function getNumber(_str){
var arr = _str.split('');
var out = new Array();
for(var cnt=0;cnt<arr.length;cnt++){
if(isNaN(arr[cnt])==false){
out.push(arr[cnt]);
}
}
return Number(out.join(''));
}
$(document).ready(function(){
$('#textbox1').on('keyup',function(){
updateTextView($(this));
});
});
//this function copies the textbox1 values to textbox value 2
$("#textbox1").bind('input', function () {
var stt = $(this).val();
$("#textbox2").val(stt);
});
You modify the function updateTextView as below:
function updateTextView(_obj) {
var num = getNumber(_obj.val());
if (num == 0) {
_obj.val('');
$("#textbox2").val('');
} else {
$("#textbox2").val(num);
_obj.val(num.toLocaleString());
}
}
And then remove the following:
$("#textbox1").bind('input', function () {
var stt = $(this).val();
$("#textbox2").val(stt);
});
In plain JS:
Try the onkeyup() event added to the first textbox. Then replace all commas in the value of the first box with nothing using value.replace(/,/g, ""). And then copy the value of the first input
function update(input) {
var value = input.value.replace(/,/g, "");
document.getElementById("second-textbox").value = value;
}
<input onkeyup="(update(this))" />
<input id="second-textbox" />
to the second.

How to manipulate value based on textbox value?

If I enter negative value in textbox, while in onclick the value will be reduced by some other value.
If I enter positive value in textbox, while in onclick the value will be add by some other values.
Make a simple comparison.
function go() {
var value = parseFloat(document.getElementById("value").value),
positiveValue = 5,
negativeValue = -1;
value += value < 0 ? negativeValue : positiveValue;
document.getElementById("value").value = value
}
<input id="value" onchange="go()">
Change the value of the textbox comparing it on button click.
HTML :
<input type="text" id="inputTextBox" />
<input type="button" id="changeButton" value="update value"/>
javaScript :
var paddingValue = 10;
document.getElementById("changeButton").onclick = function(){
var inputTextBox = document.getElementById("inputTextBox");
if(inputTextBox.value < 0){
inputTextBox.value = parseInt(inputTextBox.value) - paddingValue;
}else{
inputTextBox.value = parseInt(inputTextBox.value) + paddingValue;
}
};
jsFiddle demo

Limiting Text Input Based On Another Text Input

It seems pretty simple but I can't find a good way to do it.
I am doing a research bar which allow users to search something in terms of price mini and price maxi.
So :
I have two text input types (in html of course) "price_mini?" and "price_maxi?".
"Price_mini" cannot be bigger than "price_maxi".
How can I limit the users input of "price_mini" so that if does not allow the user to enter more than the "price_maxi" variable's input and then display an error on save(search) if the mini number is bigger than price_maxi.
Something like this should work, I couldn't get JSFiddle to handle the form to show you a good example and I don't do much in plain javascript now in days so pardon me if there is a small error or two.
HTML
<form name="myForm" onSubmit="submit()" method="post">
<input name="price_mini" type="text">
<input name="price_maxi" type="text">
<input type="submit" value="Submit">
</form>
Javascript
function submit(){
var price_mini = document.forms["myForm"]["price_mini"].value;
var price_maxi = document.forms["myForm"]["price_maxi"].value;
if(Number(price_mini) > Number(price_maxi)){
alert("Minimum price must be less than maximum price!");
}else{
// Your search code here
}
}
Imagine this html
<input id="min" type="text">
<input id="max" type="text">
This should be the correct javascript
var min = document.getElementById("min");
var max = document.getElementById("max");
min.change(function() {
if(Number(this.value) > Number(max.value)) {
this.value = max.value; // replace min with the same max value if it's bigger
}
}
Let's assume that this is your HTML.
<input id="min" type="text">
<input id="max" type="text">
The working JavaScript is this with the behavior if the max field is empty.
var min = document.querySelector('#min');
var max = document.querySelector('#max');
var calculate = function() {
if(max.value == '') return;
if(Number(min.value) > Number(max.value)) {
min.value = max.value;
}
}
min.addEventListener('input', calculate);
max.addEventListener('input', calculate);
You should compare them when they have value (min && max). If you notice that min is higher you can alert to the user or change it automatically to the lowest or to the highest.
$('.calc_input').change( function() {
var min = $('#min').val();
var max = $('#max').val();
if ( (min && max) && min > max ) {
alert('This can not be!');
// $('#min').val() = max;
// $('#max').val() = min;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Min:<input id="min" class="calc_input">
<br>
Max:<input id="max" class="calc_input">

how to make bind submit working always? it's working from time to time

I'm trying to compare to values (min - max) from two inputs
if min is greater than max should alert a message... that works
The problem is when I correct max value and submit again, it is showing the same alert...
this is my HTML:
<form>
<input type="text" placeholder="min" id="input1" class="numberInput"/>
<input type="text" placeholder="max" id="input2" class="numberInput"/>
<input type="submit"/>
<span class="msg"></span>
</form>
and this is my (relevant?) JS:
var $input1 = $("#input1");
var $input2 = $("#input2");
$(document).ready(function() {
$('form').bind("submit", function(e) {
if ($input1.val() > $input2.val()) {
alert('min value is greater than max value');
$input1.focus();
e.preventDefault();
} else {
alert('normally submitted');
evt.preventDefault();
}
})
})
You can find the full sample on http://jsfiddle.net/cruzj/Lcwhqjj7/1/
Thanks!!
.val() returns a string, so
$input1.val() > $input2.val() === true when input1 = "7" and input2 = "456456"
to avoid that comportement you have to use parseInt() function
parseInt($input1.val()) > parseInt($input2.val()) === false when input1 = "7" and input2 = "456456"
example here

Comparing two input fields

I have this function which i am using to compare two input fields. If the user enters the same number in both the text field. On submit there will be an error. Now i would like to know if there is a way to allow same number but not higher than or lower the value of the previous text box by 1. For example if user enters 5 in previous text box, the user can only input either 4, 5 or 6 in the other input field.Please give me some suggestions.
<script type="text/javascript">
function Validate(objForm) {
var arrNames=new Array("text1", "text2");
var arrValues=new Array();
for (var i=0; i<arrNames.length; i++) {
var curValue = objForm.elements[arrNames[i]].value;
if (arrValues[curValue + 2]) {
alert("can't have duplicate!");
return false;
}
arrValues[curValue] = arrNames[i];
}
return true;
}
</script>
<form onsubmit="return Validate(this);">
<input type="text" name="text1" /><input type="text" name="text2" /><button type="submit">Submit</button>
</form>
A tidy way to do it which is easy to read:
var firstInput = document.getElementById("first").value;
var secondInput = document.getElementById("second").value;
if (firstInput === secondInput) {
// do something here if inputs are same
} else if (firstInput > secondInput) {
// do something if the first input is greater than the second
} else {
// do something if the first input is less than the second
}
This allows you to use the values again after comparison as variables (firstInput), (secondInput).
Here's a suggestion/hint
if (Math.abs(v1 - v2) <= 1) {
alert("can't have duplicate!");
return false;
}
And here's the jsfiddle link, if you want to see the answer
Give them both IDs.
Then use the
if(document.getElementById("first").value == document.getElementById("second").value){
//they are the same, do stuff for the same
}else if(document.getElementById("first").value >= document.getElementById("second").value
//first is more than second
}
and so on.

Categories

Resources