Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple form Validation

Status
Not open for further replies.

MrRich36

Technical User
Nov 20, 2009
6
US
Hello,
I am new to JavaScript and I am looking for a function that will check a user's input and changing that input if necessary.

i.e.
there is a form field with an initial value of 1, if the user chooses to change the number it can only be numbers from 1-10. I would like onblur for the function to run and if the number entered was greater than 10 the value defaults to 10 or if the number entered is 0 it defaults to 1.
I don't have any code to show as I don't know where to start with it.

Thanks in advance for your help.
 
I'd start with putting together a function, seeing as you know about the onblur event, calling it should not be difficult.

This should get you started:

Code:
<script>
function Validate_Input(myinput){
alert(myinput.value);
}
</script>


<input type=text name="mytextbox" onBlur="Validate_Input(this);">

Basically this calls the Validate_Input function, and passes the "this" object whcih acts as a pointer to the element where this call is made.

Inside the function you can use the passed object to validate what you need.


This just produces an alert box with the current value of the input.

You should be able to piece together a couple of If statements to check for the values you need.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top