Jump to content
Sign in to follow this  
choobakka

no numbers allowed

Recommended Posts

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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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) {

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...