I want to validate an ASP.TextBox for entering only Integer Value with javascript function. In case of non-Integer value entered by a user, it should displays a warning message and keeps the focus on current element. I used the following procedure:
On Web Page’s Asp.TextBox element:
onchange="return isInteger(this);" onblur="return isInteger(this);"
And valdating function in functions.js file
function isInteger(mobj) {
var mval = mobj.value;
if (isNaN(parseInt(mval))) {
alert("Plese Enter a Valid Integer...!");
mobj.focus();
return false;
}
return true;
}
Is it correct, or is there any correct way to do the validation. Kindly suggest.
Yes idea is correct. Thats how you have to validate your textbox.
Validating a text box value using javascript
http://social.msdn.microsoft.com/Forums/en-US/netfxjscript/thread/07a91c1d-34db-44d8-9bd6-a424394a7028/
Or there is another way using onKeyUp() on text box, check the value of textbox, and return false if input is not an integer.
You can use ASP.NET RegularExpressionValidator Control for validating your text box input value.
Eg.
<asp:TextBox id="textbox1" runat="server" />
<asp:RegularExpressionValidator
ControlToValidate="textbox1"
ValidationExpression="\d{1,10}"
ErrorMessage="Plese Enter a Valid Integer...!"
runat="server" />
The idea is correct but onchange is enough. onblur is not necessary because you don't want to validate each time the control lost focus(i.e. tab through or just click) without having any change.
onblur is enough for validation here..
Using onchange and onblur together will create two alerts sometimes i.e when focus is lost and onchange fires together...
Try here...
If you use onchange alone, you will get alert only first time you lose the focus when you keep text unchanged ..like here... or else your have to clear textbox value if it is invalid...
onkeyup can be a best alternative but still you may need to clear textbox value like here...
Related
On my page I have a textbox and I connected three validationcontrols to it.
Two comparevalidators for minimum and maximum values and one requiredvalidator.
If the user changes the contents of the textbox, the validation is not done until the user clicks somewhere else in the form. Either on another control or some space of the page.
I would like to validate while he is changing the contents with javascript as I don't want a postback on each character he types.
How can I can I call the requiredvalidators IsValid function from within the OnTextChanged event?
<asp:TextBox ID="txtMoveSparesAmountToMove" runat="server" CssClass="txtMoveSparesAmountToMove" OnClick="this.style.backgroundColor='white'" OnTextChanged="**HERE THE VALIDATE CALL**"></asp:TextBox>
By validating during input I want to remove the validators text the moment the contents is valid.
In this textbox the user has to input a number. I he did so and then clears the box, the validation appears when the focus leaves the box. The validation does not go away the moment he puts in a number again until the focus leaves the box.
I got comments on that from the users, they don't want to see the red validation text as soon as they correct it.
How do i restrict user to enter any data from Keyboard to Text-box. Actually my requirement is that, this particular textbox is being used for only scanning purpose. Hence, only scanned data will be entered to this textbox and want to restrict any manually entered data from keyboard.
Just disable the textbox and get the value from the backend will solve the issue.
SAMPLE FIDDLE
I dont think there is any need of javascript here.
Just use readonly in you html.
<input value="somevalue" name="meow" readonly>
Thats it.No fancy javascript needed.
Try this
$(function(){
$('input[type=text]').bind('keyup keydown keypress', function (evt) {
return false;
});
});
Demo
If you want to fill data from backend then you can permanently disable
for that you just need to add in textbox html readonly and then fill value by
document.getElementById("yourtextbox_id").value="your_scanned_value";
I am making a small calculator (just for JavaScript learning) and I have two input fields for fraction calculation:
Field: numerator
Field: denominator
And a button "Calculate fraction" which executes the function myFraction(numerator,denominator) when user clicks on it.
Is it possible to do the same without the Button?
I mean JavaScript should recognize that someone is making input and then calculate the fraction automatically.
You can hook into the onkeyup or onkeydown event handlers and call your function:
<input type="text" id="numerator" onkeyup="myFraction()" />
<input type="text" id="denominator" onkeyup="myFraction()" />
You'll need to grab the values of the text boxes inside the myFraction() function and check to see if they're blank before you output the fraction value to the screen though.
function myFraction() {
if(document.getElementById("numerator").value != "" &&
document.getElementById("denominator").value != "") {
...do calculations
}
}
Sure, I think you need onChange() (as the text is being edited) or onBlur() (when the textbox looses focus). See the difference here.
Since you're studying, jQuery can help with some of the nuances - including here.
Javascript is event driven, so yes, you can catch the event after they are typing with keyup or when the user moves off from a textbox control.
You could check the length of a textbox after they leave it, validate what they entered and then run the calculation.
I usually use blur to catch after a user leaves a textbox. I usually use Onchange for select.
When typing into a <input type="text"> field I would like to precheck every entered value before it appears on the screen. E.g. if the user enters any non numeric value, nothing will happen and only if he enters a numeric value the field will change.
Which is the right event to use keydown(), keyup() or is there something better? How do I cancel the current change of the text field without having to remember the old value and manually resetting it?
You could bind to the keypress event and then check the character represented by the which property of the event object. If it isn't a number you can use preventDefault to prevent the default behaviour of writing the character to the element:
$("#someInput").keypress(function(e) {
if(isNaN(String.fromCharCode(e.which))) {
e.preventDefault();
}
});
Here's a working example.
I have a form that uses a servlet I don't have access to for validation. The powers that be want the field to be prefilled with a value "Enter promotion code" - unfortunately, since placeholder doesn't work with IE, the field is going through the validation process on submit for the default text.
Is there a way to clear the value on that field if the value equals the default text when the form is submitted? (jquery or javascript)
thanks!
You should toss this somewhere on load.
If you aren't using jQuery on the page yet, it'll look like this:
$(function() {
// when the submit button is pressed
$('#yourForm').submit(function() {
if($('#yourInputBox').val() == YOUR_DEFAULT_STRING)
$('#yourInputBox').val(''); // clear the value
return true; // submit the form
});
});
You haven't provided any code to work with, but conceptually you can hook the submit function for your form with javascript, check to see if the prefilled value is still in the form and clear it if so and then allow the submit to proceed.