hello friends ....for validating name field i am using below function.....
function checkName(str)
{
var name = new RegExp("^[a-zA-Z .]+$");
if (!str.match(name))
alert('Invalid characters in Name.');
}
<label>4. Name</label>
<input type="text" name="name" size="30" maxlength="30" onBlur="checkName(this.value)" >
but from this function i can validate only after i put curshor on next field......but i need this in such a way that as soon as i enter numeric value it shold show the error message i.e
praka5
as soon as i enter 5 it should show the error message and clear the all numeric part and remain with praka....
please suggest me any javascript or event handler to solve this problem
Help to validate filed
Moderator: General Moderators
Re: Help to validate filed
you are using "onBlur" to call the function. onBlur will only run when the object loses focus. I think what you are looking for is "onKeyUp" (that is, as soon as the key is lifts up).
Also, "if(!str.match(name))" will trigger whenever name is blank, so you might want to add that in your if-statement.
So your final code should be something like this:
Also, "if(!str.match(name))" will trigger whenever name is blank, so you might want to add that in your if-statement.
So your final code should be something like this:
Code: Select all
<script type='text/javascript'>
function checkName(str)
{
var name = new RegExp("^[a-zA-Z .]+$");
if (!str.match(name) && name!='')
alert('Invalid characters in Name.');
}
</script>
<label>4. Name</label>
<input type="text" name="name" size="30" maxlength="30" onBlur="checkName(this.value)" >
-
om.bitsian
- Forum Commoner
- Posts: 36
- Joined: Wed Sep 09, 2009 4:13 am
Re: Help to validate filed
thanks rburgens.........."onKeyUp" is right to use in my case
you are right I want to use name!=' ' this in my code but name!=" " is working for me.......name!=' ' is not working
problem: now i am using "onKeyUp" , its showing the alert box but i am not able to clear the numerical part......i.e
as soon as i enter any number it should show the alert box and after that when i click on "Ok" it should clear that number and show the remaining string like
pineapl3 ........here 3 should clear out and "pineapl" remain in the form
thank you
you are right I want to use name!=' ' this in my code but name!=" " is working for me.......name!=' ' is not working
problem: now i am using "onKeyUp" , its showing the alert box but i am not able to clear the numerical part......i.e
as soon as i enter any number it should show the alert box and after that when i click on "Ok" it should clear that number and show the remaining string like
pineapl3 ........here 3 should clear out and "pineapl" remain in the form
thank you