Avatar of emzi19
emzi19

asked on 

Javascript Numeric Form Validation

Hi, I have some Javascript form validation.. but how can i check a field to see if the imput is numeric?
function validateForm(form) { //This is the name of the function
 
  
    var emailID=form.email;
    if (form.ORDERREF.value == "") { //This checks to make sure the field is not empty
   alert("Please Fill in your purchase order reference, this is important so we can process your order."); //Informs user of empty field
   form.ORDERREF.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
    if (form.partno1.value == "") { //This checks to make sure the field is not empty
   alert("You Must Enter At least 1 Part Number."); //Informs user of empty field
   form.partno1.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
   if (form.account.value == "") { //This checks to make sure the field is not empty
   alert("Please Fill in your account code correctly, this is important so we can process your order."); //Informs user of empty field
   form.account.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
      if (form.ADDRESS1.value == "") { //This checks to make sure the field is not empty
   alert("Please Fill in your first address line."); //Informs user of empty field
   form.ADDRESS1.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
         if (form.DELIVERYADDRESS1.value == "") { //This checks to make sure the field is not empty
   alert("Please Fill the first address line for delivery."); //Informs user of empty field
   form.DELIVERYADDRESS1.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
    if (form.contact.value == "") { //This checks to make sure the field is not empty
   alert("Please give us a contact name so we can call you if there are any problems with your order."); //Informs user of empty field
   form.contact.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
    if (form.phone.value == "") { //This checks to make sure the field is not empty
   alert("Please give us a contact phone number so we can call you if there are any problems with your order."); //Informs user of empty field
   form.phone.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
    if ((emailID.value==null)||(emailID.value=="")){
        alert("Please Enter your Email ID")
        emailID.focus()
        return false
    }
    if (echeck(emailID.value)==false){
        emailID.value=""
        emailID.focus()
        return false
		}
				 	if ((form.email.value !== form.email2.value)) {
	alert ("Your email address is not matching")
	form.email2.focus();
	return false;
	}
     if (form.message.value == "") { //This checks to make sure the field is not empty
   alert("Enter the postcode where you want the item dispatched to in the message field."); //Informs user of empty field
   form.message.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   }
    if (form.message.value == "Your Order Reference , delivery postcode and Message") { //This checks to make sure the field is not empty
   alert("Enter the postcode where you want the item dispatched to in the message field."); //Informs user of empty field
   form.message.focus( ); //This focuses the cursor on the empty field
   return false; //This prevents the form from being submitted
   
    }
    return true
 }
</SCRIPT>

Open in new window

JavaScript

Avatar of undefined
Last Comment
Morcalavin
ASKER CERTIFIED SOLUTION
Avatar of flipz
flipz
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

You can use a regular expression on your value to test if it contains only numbers:
var num = '1234567'; //try adding anything but numbers here
if(!num.match(/^[0-9]+$/))
{
alert('Please enter a number')
return false;
}
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

flipz suggestion will match positive/negative ints and floats.  Mine will only  match positive integers, so bear that in mind.  You may only want one or the other, or both.
Avatar of emzi19
emzi19

ASKER

Thanks Very Much
Avatar of Morcalavin
Morcalavin
Flag of United States of America image

Wouldn't this work better, since the other function would match '-123....34...-3'


function IsNumeric(sText)
{
   if(!sText.match(/^-?[0-9]+\.?([0-9]+)?$/))
   {
      return false;
   }
   return true;
}
if (!IsNumeric('-123.6'))  //replace this number with your value you want to verify
{
   alert('The phone number is not numeric.');
}

Open in new window

JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo