-3

I currently have a phone number validation script that has been written in JavaScript, What I am looking to do is translate my code from JavaScript to PHP as the rest of the website does all validations once submitted in PHP.

The format rules should be as follows: 1st and 2nd number must be 0 n 3 respectively. and 3rd number can be either 0,1,2,3, or 4

<script>
   function validation(){
   var a = document.getElementById("contact").value;
   if((a.charAt(0)!=0) || (a.charAt(1)!=3) || ((a.charAt(2)!=0) && (a.charAt(2)!=1)&& (a.charAt(2)!=2) && (a.charAt(2)!=3) && (a.charAt(2)!=4)))
       {
       document.getElementById("message").innerHTML="Contact number is invalid.";
       return false;
      }
  }
      </script>
DataCure
  • 425
  • 2
  • 15
  • So you can not change that over to be validated in your php code? What part are you not sure about? – epascarello Jan 25 '19 at 14:24
  • Simply replace all `a.charAt(#)` with `$a[#]` – Justinas Jan 25 '19 at 14:24
  • can you show an example of validation you want ? – Yassine CHABLI Jan 25 '19 at 14:24
  • @Justinas you are assuming the developer is using jQuery and that would fail instantly if the developer has not included a jQuery library call. – DataCure Jan 25 '19 at 14:26
  • 1
    @DataCure OP asks for PHP, so it's in PHP and not some jQuery code. – Justinas Jan 25 '19 at 14:28
  • @Justinas That as maybe, but the OP has presented us with JavaScript not PHP, hence the comical post made by treyBake stating thats some funny PHP you have there – DataCure Jan 25 '19 at 14:29
  • @DataCure The OP also asked "*Now i wanna set this script to be in php*"; safe to assume they are attempting to translate to PHP. – esqew Jan 25 '19 at 14:30
  • @DataCure my funny comment is more a comment on the lack of attempt – treyBake Jan 25 '19 at 15:29
  • @Justinas ur code is helpful... but i have a problem with is_nan... i use is_nan just like this and is not working... any other method to use in php ,,, or i have made some mistakes else if(is_nan($contact)) { $msg10="
    Please enter numeric Contact.
    "; }
    – Muhammad Hassan Jan 27 '19 at 06:54
  • @MuhammadHassan Try `empty($contact)` – Justinas Jan 27 '19 at 09:34
  • I tried all the validation,,, all working fine... but now i wan to take only numeric value... so i used 'is_nan' in php,, and is not working... is there any method to not accept alphabet in input.. in php? – Muhammad Hassan Jan 27 '19 at 09:49

2 Answers2

0

To employ the same idea you've illustrated in your Javascript code above, you can use the square bracket notation on a string variable to test each position:

function validation($contact)
{
    return intval($contact[0]) == 0 && intval($contact[1]) == 3 && intval($contact[2]) >= 0 && intval($contact[2]) <= 4;
}

Repl.it

You can alternatively achieve this functionality more efficiently and elegantly with regular expression matching:

function validation($contact)
{
    return preg_match('/^03[0-4]/', $contact) === 1
}

Repl.it | Regex101

esqew
  • 42,425
  • 27
  • 92
  • 132
0

Consider using regular expressions to validate your data. In PHP:

$rgx = '/^03[0-4]/';   // the expression 

if(preg_match($rgx, $your_number) === 1){
    .....
} 

034xxxxxxxwill match

035xxxxxxx won't

Here's some useful sites if you want to delve in further to regex:

Travis Heeter
  • 13,002
  • 13
  • 87
  • 129