Jump to content

if not expression


p3aul

Recommended Posts

Hi!

i am wanting to veryify data from a form textfield. All I want is digits like this 99.9 with a minus sign optional OK so I can use regular expressions and preg_match() for this.

 

My problem is I want to use an if expression and popup an alert box if the match is not successful(ie there is something other than a number , (-) sign or a period) here is what i have:

 

preg_match('/^\d+$/', $db)

if ( !=  preg_match('/^\d+$/', $db));{

echo '<script language="javascript">confirm("Must be numbers only!")</script>;'; }

 

but this just causes an error page with this:

Parse error: syntax error, unexpected T_IF in C:\xampp\htdocs\Untitled-2.php on line 71

 

This almost as clear as mud!

Any help would be gratefully appreciated!

Paul

 

 

 

Link to comment
Share on other sites

OK I saw my mistake about the Exclamation point! apparently PHP doesn't use this as a symbol for *not* in and if statement. I corrected it to:

 

if (preg_match('/^\d+$/', $db)== false);{

echo '<script language="javascript">confirm("Must be numbers only!")</script>;'; }

 

and added an "else" statement but it still doesn't work! In the interest of keeping the post short I have uploaded the relevant code. Bear in mind that I only verify one variable($db) for testing purposees. If I can get this worked out the rest should follow. My math maybe convoluted, but I know it works!

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ok, I finally got it to work with ! used in the if expression. But now I can address another issue.

 

When the page loads, somehow the code falls through to the javascript alert box first. I click ok and the code functions normally. This happens with what ever I put as the initial value of db in the textfield. I've tried it with blanks, zeros, and 111. It still falls through. Here is the little bit of code where I have my problem

 

$db = $_POST["db"];
$wb = $_POST["wb"];
$pr = $_POST["pr"];
//
// For testing purposes only I'm only verifying $db
//

if ( ! preg_match('/^\d+$/', $db)){
echo '<script language="javascript">confirm("Must be numbers only!")</script>;'; } 
// Calculate Ew, the wetbulb vapor pressure component
else {
$F = 6.112 * exp((17.67*$wb)/($wb+243.5));

Link to comment
Share on other sites

extra semicolon

echo '<script language="javascript">confirm("Must be numbers only!")</script>;'; }

 

Well that was the bit of code I copied from a forum asking how to use an alert box in PHP. On the face of it it makes sense, php is echoing javascript code and that code requires a semicolon also. then the PHP code needs a semicolon to complete it. In any case the point is moot. The javascript still slips through on load with or without the semicolon and I get no error in either case.

 

I'm still looking  :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.