Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:03:42+00:00 2026-05-30T00:03:42+00:00

I try to validate fields. Three fields that may only contain numbers. The fields

  • 0

I try to validate fields. Three fields that may only contain numbers. The fields have the variable names $num1, $num2, $num3. I have put them in an array and then I run preg_match but it does not work. How should I do?

$valnum = array (‘$num1’, ‘$num2’, ‘$num3’);
    if (preg_match('/[0-9]/', $valnum)){
echo $mycost;
}
    else {
echo 'You can only enter numbers';  
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:03:43+00:00Added an answer on May 30, 2026 at 12:03 am
       is_numeric() 
    

    would be a better approach

    //worked up example:

    <?php
    $num1 = '1';
    $num2 = '11';
    $num3 = '11.11';
    
    $mycost = '$99.99';
    
    $valnum = array($num1,$num2,$num3);
    $check = '';
    foreach($valnum as $element){
        if(!is_numeric($element)){
            $check = 'bad';
            break;
        }
    }
    
    if($check=='bad'){
        echo 'You can only enter numbers';
    }else{
        echo $mycost;
    }
    

    ?>

    returns 99.99

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model with some fields. I have a model form that represents
try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that
Is it possible to have Required Field Validator controls to validate more than one
I wrote my custom jquery validation script to validate the fields in the form
I have a table name Discount that has the following schema: PK DiscountID int
I am trying to validate a form in that if there is no text
I have two questions about Javascript: How can I prevent my fields from being
I need to validate that certain variables can create a valid datetime and if
I have some terribly written ASP.NET code that is just not working right (go
I was searching for a method to validate my form fields in symfony via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.