I have written mobile number validation script as you can see in below.the problem it is working fine with the URL1.but not with the URL2.I want to know why is that?
URL1 - http://axbc.com/con/s2.php
URL2 - http://axbc.com/con/s2.php?key=d908e8401774411861043
Here is my Code
if(isset($_POST['btn-signup']))
{
$mobilenumber = $_POST['mobilenumber'];
if(!empty($dialogmobilenumber)) // phone number is not empty
{
if(preg_match('/^947\d{8}$/',$mobilenumber)) // phone number is valid
{
echo 'success';
}
else // phone number is not valid
{
echo "<script>alert('Mobile Number is Not valid.. Format should be ');
</script>";
}
}
}
Your code is working fine for me in both urls.
It is doing, what it is meant for, It is matching all the 11 digit numbers starting from '947'+8 nos.
As, you are not using the passed variable anywhere in this piece of code,So it can never be the cause of your error.
One thing,You are receiving mobilenumber as POST in $mobilenumber variable.
So,why are you using $dialogmobilenumber to check if it is empty or not ?
Have you defined this anywhere in the program.
if(!empty($dialogmobilenumber)) // phone number is not empty
{ ...
Another thing, Is your form also present in s2.php ?
If not, what is your action contains (action="?") for the form ?
Note: If you can paste the complete code and output that you are getting for
URL2, that will be good.
Related
Im making an email verification system on PHP in cPanel, and when i press register, it sends an email to me with a link like this "../verified.php?code=1cbb402a59e8ec26dac0", i would need to get the full link and would have to chop it so it leaves me with just the code "1cbb402a59e8ec26dac0" so i can check if the code exists in database and then verify the account.
So from this
../verified.php?code=1cbb402a59e8ec26dac0
To This
"1cbb402a59e8ec26dac0
Purchasing the hostings for the first time fried my brains, so would be thankful if anyone could help me,
For getting the text after the code: in the link, you can use the PHP $_GET function. You can use this code in your verified.php to get the text after code:
<?php
if(isset($_GET['code']))
{
$Data=$_GET['code'];
}
else{
echo "Invalid Email Verification";
?>
Now the part after the code= gets stored in the variable Data.
You can change that to your desired variable.
Sometimes even when the code is set, it might be empty, so to check that, the empty() function in PHP can be used.
You can add this code to your verified.php:
<?php
if (empty($Data))
{
echo "Invalid Verification ID";
}
else
{
echo "Email Verification Success";
//your other codes to update it on the server
}
?>
Well you have chosen to pass parameters which will be available in the global $_GET[] (as opposed to $_POST[]).
So in your verified.php you will need to examine for the presence of $_GET['code'] and take the appropriate action.
Using $code = $_GET['code']; is very bad as you need to qualify it.
At a minimum you would need to...
<?php
// Ternary to give $code a value to prevent undefined variable error.
$code = isset($_GET['code']) ? $_GET['code'] : NULL;
if($code === NULL){
// error
} else {
// Check its a valid code and take the appropriate action.
}
I currently have this code below which validates username length only. If none entered it will show error message, if less than 3 characters entered show error message. I want to add an if/else statement that if the user enters special characters like !##$%^&*()+=/? etc... the only special character is allowed is underscore (_) and hypen (-)... Help me how.
thanks
here's the code i have:
<?php
$serror="";
if (isset($_POST['submit'])) {
$username=$_POST['username'];
$lengt = strlen($username);
if($lengt == 0){
$serror=" Please enter account username ";
}
else{
if($lengt < 3 ){
$serror=" Please enter valid account username ";
}
}
if($serror==""){
ob_start();
echo "Success";
header("Location:progress.php?username=$username");
exit;
ob_end_flush();
}
else{}
}
?>
use preg_match() function
$yourString = "blahblah";
if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) {
#your string is good
}
remeber preg_match() returns boolean
Your php script works after the user submits the form. From your tags in the question I assume you can use javascript. With Javascript you catch these errors before the form is submitted.
So, your html input field would fire a script and you can use the onkeypress event to see the value of the keystroke.
The submit button would also have a javascript event to look for min string length, else give warning and not submit form.
As others already pointed out, you should use regular expressions for this.
Try with the following if-statement (allows a-z, numbers, underscores and hyphens). It also checks that the length is at least 3 characters:
if (!preg_match("/^([\w\-]{3,})$/", $username)) {
$error = "Not enough chars or there are invalid ones".
}
Read more about preg_match() here
So I have a form where 2 out of 6 fields are visible to the user. The user can then click a button to reveal the other fields.
Each field uses the following PHP validation (Note: the preg_match is there to make sure they have entered a space as it's a full name field):
$multipleFormErrors = array();
if (!isset($firstGuestName) || empty($firstGuestName) || !preg_match("/ /",
$firstGuestName)) {
$multipleFormErrors["firstGuestName"] = "You have not entered your full name.";
}
if (!isset($secondGuestName) || empty($secondGuestName) || !preg_match("/ /",
$secondGuestName)) {
$multipleFormErrors["secondGuestName"] = "You have not entered guest #2's full
name.";
}
if (!isset($thirdGuestName) || empty($thirdGuestName) || !preg_match("/ /",
$thirdGuestName)) {
$multipleFormErrors["thirdGuestName"] = "You have not entered guest #3's full name.";
}
And so on up until guest #6.
The results are then being echoed to the user using:
if (isset($_POST["multipleSubmit"])) {
if ($multipleFormErrors) {
echo "<div class=\"errors\">";
echo "Please fix the following errors:";
echo "<ul>";
foreach ($multipleFormErrors as $error) {
echo "<li>";
echo $error;
echo "</li>";
}
echo "</ul>";
echo "</div>";
}
}
The issue here is that all of the errors will display even if guests 3 - 6 aren't visible to the user. So If they submit the form with just the initial 2 guests filled out they will get an error because guests 3 - 6 have a value of an empty string. I think a way around this would be for PHP to detect whether the display value is set to block like you can do in JS so is this possible or do I need to do something different?
Cheers!
PHP happens on the server, Javascript happens on the client, and that's the crux of your issue. The server has no way (without a lot more coding and state tracking) to know if the client is looking at something or not.
I recommend:
Keeping your general application structure the way it is (don't do that state tracking, which would require a lot more JS/jQuery/etc)
Perhaps put your error code with the text box, so that the error only shows if the text box does
Code your system with the full realization that guests (beyond the first?) are optional, so guest checking should only occur on server side if there is a partial name (As it is, the error shows if the guest is blank, which will often happen). A blank name for guests 2-6 is probably completely legitimate.
I have a jobs site , and my visitors are able to add jobs on the front end of it , i need to check the job details to see if it has a phone number within it or not using php code
example of the 6 digit numbers could be 123456 , 533434 and no other format,
if there is any 6 digit numbers an error message would show up to make visitors remove it from the details section
I need something like this :
if (strpos($_POST[details],'######') !== false)
{
echo 'remove the phone number from job details';
}
if (preg_match('/[1-9]\d{5}/',$_POST[details])) {
echo 'remove the phone number from job details';
}
You have to use a regular expression like this: \d{6}
if (preg_match('#\d{6}#', trim($_POST['details']))) {
// Then the phone number is valid
}
I would like to accept only small and capital letters from the user.
I tried the below code, it echoes the invalid character message but doesn't work. I mean it doesn't check. It just displays the message. Any help?
<form action="" method="post">
<input type="text" name="fname">
<input type="submit" value="Send" name="submit">
</form>
Update: this is what I have to check and insert the name to database. if numbers found in the name reject the name by displaying the error message else if the name contains only letters insert it into database. That's all I want to acheive.
<?php
if ( isset( $_POST['submit'] ) ) {
$fname = $_POST["fname"];
if(!preg_match ('/^([a-zA-Z]+)$/', $fname)){
echo "Invalid characters";
}
if (empty($fname)) {
echo '<span> First name is required</span>';
}
else{
$mysqli = new mysqli("localhost", "root", "", "test");
$stmt = $mysqli->prepare("INSERT INTO test (firstname) VALUES (?)");
$stmt->bind_param("s", $fname);
$stmt->execute();
$stmt->close();
$mysqli->close();
}
}
?>
If you just want to check you could use ctype_alpha() but you said you want to ACCEPT only letters so if you choose to accept the input you could:
$fname=preg_replace('/[^a-z]/i','',$fname);
better after the check
if(!isset($_POST['fname']) || !ctype_alpha($_POST['fname'])){
// can i haz alpha letters only?
}
(reference)
There are several issues with the code, and the one you are stuck with is probably that you have the form and its processing in the same PHP file. That’s possible, but it requires a different approach. For a starter, it’s probably better to separate them.
What happens with the code posted is that the PHP processor tries to process the form data when no form has been submitted, without even checking for the presence of the data. Now $fname is undefined, so the test always fails.
The test is wrong, too. Now it only checks whether $fname contains at least one letter. For example, if(!preg_match ('/^[a-zA-Z]+$/', $fname)) would test that $fname consists of one or more Ascii letters and nothing else.
use this , this is giving me correct answer
if(!preg_match ('/^([a-zA-Z]+)$/', $fname)){
echo "Invalid characters";
}
else{
echo "correct";
}
The general idea of checking for characters that don't match the [a-zA-Z] pattern is a good one.
However, the "not" part of your if condition is in the wrong place if you want this to work. What you've got now just makes sure that any single character in fname is an upper- or lower-case Latin letter.
You want to push the "not" part of the logic into the pattern:
if (preg_match('/[^a-zA-Z]/', $fname)) {
This checks if any character in fname is not a Latin letter, which is what you're trying to do.
Edit: Your new update has a different test that also works (it appears to be from sourcecode's updated answer, but you've got several tests from the different answers here that will work equally well). But, your updated post makes it clear that your problem isn't really with the pattern for testing the name.
Your code looks like this:
if (/* invalid fname */) {
echo "Invalid characters";
}
if (/* empty fname */) {
echo '<span> First name is required</span>';
}
else {
/* insert into database */
}
That else clause only depends on the the if that comes immediately before it: the check whether fname is empty. In other words, regardless of the result of your check against the characters of fname, you insert it into the database whenever it's not empty.
One easy way to fix this is to just change your second if to an elseif. This will chain all three conditionals together, so the final else block will only occur if both of the earlier conditionals that print error messages weren't triggered.
if (/* empty fname */) {
echo 'First name is required.';
}
elseif (/* invalid fname */) {
echo 'Invalid characters';
}
else {
/* insert into database */
}