choobakka Posted March 24, 2006 Share Posted March 24, 2006 i have a form with text fields for first name, last name and email. i would like the first 2 boxes to be validated by checking if there are any numbers entered in the boxes. if numbers are entered, i would like an alert. im not too worried about the alert, just checking for number.any ideas?thanks in advance Link to comment Share on other sites More sharing options...
Webworldx Posted March 24, 2006 Share Posted March 24, 2006 if(input.value.match(/\d/) != null){ alert('Contains a number');} Link to comment Share on other sites More sharing options...
choobakka Posted March 24, 2006 Author Share Posted March 24, 2006 the problem is that i already have other validation for the same text box(s):if (lastname.length == 0) {returnValue = false;alert("PLease enter your first name");then i inserted the above example as follows:if (lastname.length == 0) {returnValue = false;alert("PLease enter your first name");if (firstname.value.match(/\d/) != null) {alert('Contains a number');}}i also also put in the folowing:if (lastname.length == 0) {returnValue = false;alert("PLease enter your first name");if (firstname.value.match(/\d/) != null) {returnValue = false;alert('Contains a number');}either way it does now work?any ideas?im trying to nest the second validation (numbers) within the first validation (checking for any input) Link to comment Share on other sites More sharing options...
Webworldx Posted March 24, 2006 Share Posted March 24, 2006 I'm taking it that firstname is the actual field, rather than a variable? If it is a variable, remove the .value bit and it'll work.if (firstname.match(/\d/) != null) { Link to comment Share on other sites More sharing options...
choobakka Posted March 24, 2006 Author Share Posted March 24, 2006 in my form the text field is called firstName. in the javascript i have declared a variable as follows:var firstname = frmShuffle.firstName.value; Link to comment Share on other sites More sharing options...
Webworldx Posted March 24, 2006 Share Posted March 24, 2006 Then it's the change i made in my second reply then Link to comment Share on other sites More sharing options...
choobakka Posted March 24, 2006 Author Share Posted March 24, 2006 neither changes work Link to comment Share on other sites More sharing options...
Webworldx Posted March 24, 2006 Share Posted March 24, 2006 Say your boxes had:First Name: Jared01Last Name: <blank>Your first code: var firstname = frmShuffle.firstName.value; var lastname = frmShuffle.lastName.value; if (lastname.length == 0) {returnValue = false;alert("Please enter your first name");if (firstname.match(/\d/) != null) {alert('Contains a number');}} Would bring up both alertsIf you had:First Name: Jared01Last Name: SwintonYou'd have no alert boxes, due to the way you've embedded one inside the other. Link to comment Share on other sites More sharing options...
choobakka Posted March 24, 2006 Author Share Posted March 24, 2006 so how would u suggest i do it then:at the moment i have code which checks if first name is empty. if it is then message saying "please enter first name".if it is not, i have code to check if last name is empty. if it is then message saying "please enter last name"if it is not, i have code to check if email is empty, if it is, then message saying "please enter email". if it is not then i have a do/while with a prompt for the user to enter their email again (validation against their original email input)after all of this is satisfied, i run another function.i need to be able to check if the first name and last name have numbers and if so throw an error. how could i best do this?id prefer to do it nested for each text field separately. ie check if empty then check if numbers, then if satisfied move on to last name. Link to comment Share on other sites More sharing options...
Webworldx Posted March 24, 2006 Share Posted March 24, 2006 function checkAll(){ var firstname = frmShuffle.firstName.value; var lastname = frmShuffle.lastName.value; if(firstname.length == 0 || lastname.length == 0){ alert('One or more fields has been left blank'); return false; } if (firstname.match(/\d/) != null) { alert('The first name field contains an integer'); return false; } .... more if's here} Hope that makes sense Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now