-5

I have this regex as follows

/^\S(?=.*[a-z])(?=.*[A-Z])(?=.{8,})(?=.*[0-9])\S

it does most of what's its suppose to do such as must be eight characters long have at least one uppercase character one lowercase and one number however the problem is that it accepts any special characters at the end of the string even it passes the other validation i can't have this can someone modify this regex that it keeps the same functionality mentioned before but does not accept any special characters at all. Oh this is for php and javascript i use this both for client side and server side validation of a password any clues as to where the regex fails is greatly appreciated.

Theodore
  • 49
  • 2
  • 7

2 Answers2

0

You should use a bunch of RegExps for that:

/* javascript */
if(yourString.length > 7 && /[A-Z]/.test(yourString) && /[a-z]/.test(yourString) && /\d/.test(yourString)){
  // pass - ajax to test on server if done with submission
else{
  //fail
}

PHP validation, assuming you have passed the correct post values to the Server via AJAX, could look like:

<?php
$response = array('good' => false);
if(isset($_POST['eight_or_more']){ // you can ajax string as any valid prop name
  $eom = $_POST['eight_or_more'];
  if(count($eom) > 7 && preg_match('/[A-Z]/', $eom) && preg_match('/[a-z]/', $eom) && preg_match('/\d/', $eom)){
  $response['good'] = true;
}
echo json_encode(response); // now JavaScript should look for resp ajax data argument - if(data.good){ }
?>
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?:[a-zA-Z0-9]{8,})$/ (click for diagram)

I assumed you meant at least 8 characters. If not, then you need {8} (exactly 8) instead of {8,} (at least 8)

I assumed "no special characters" means only alphabetic and numeric characters, [a-zA-Z0-9] if any other characters are to be allowed, then you can add them here.

Tests here: https://regex101.com/r/QW2qbo/1/

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46