p3aul Posted October 9, 2008 Share Posted October 9, 2008 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 https://forums.phpfreaks.com/topic/127632-if-not-expression/ Share on other sites More sharing options...
trq Posted October 9, 2008 Share Posted October 9, 2008 if (!preg_match('/^\d+$/', $db)) { Link to comment https://forums.phpfreaks.com/topic/127632-if-not-expression/#findComment-660424 Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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 https://forums.phpfreaks.com/topic/127632-if-not-expression/#findComment-660547 Share on other sites More sharing options...
DarkWater Posted October 9, 2008 Share Posted October 9, 2008 PHP does indeed use ! as its NOT symbol. The reason for the first error was due to the fact that you didn't terminate the (very random) preg_match() on the line above the IF with a semicolon. Link to comment https://forums.phpfreaks.com/topic/127632-if-not-expression/#findComment-660556 Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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 https://forums.phpfreaks.com/topic/127632-if-not-expression/#findComment-660588 Share on other sites More sharing options...
CroNiX Posted October 9, 2008 Share Posted October 9, 2008 extra semicolon echo '<script language="javascript">confirm("Must be numbers only!")</script>;'; } Link to comment https://forums.phpfreaks.com/topic/127632-if-not-expression/#findComment-660615 Share on other sites More sharing options...
p3aul Posted October 9, 2008 Author Share Posted October 9, 2008 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 https://forums.phpfreaks.com/topic/127632-if-not-expression/#findComment-660621 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.