2

I have some javascript written to validate that a string is alphanumeric but i was just wondering how i could add some code to include hyphens(-) and slash's(/) as acceptable inputs. Here is my current code:

function validateAddress() {
  var address = document.getElementById('address');

  if (address.value == "") {
    alert("Address must be filled out");
    return false;
  } else if (document.getElementById('address').value.length > 150) {
    alert("Address cannot be more than 150 characters");
    return false;
  } else if (/[^a-zA-Z0-9\-\/]/.test(address)) {
    alert('Address can only contain alphanumeric characters, hyphens(-) and back slashs(\)');
    return false;
  }
}
Oktav
  • 2,143
  • 2
  • 20
  • 33
AJJ
  • 899
  • 3
  • 11
  • 16

5 Answers5

13

Simply add them to the character group. Of course, because both - and / are special characters in this context (/ ends a RegExp, - expresses a range), you'll need to escape them with a preceding \:

function validateAddress(){
    var TCode = document.getElementById('address').value;

    if( /[^a-zA-Z0-9\-\/]/.test( TCode ) ) {
        alert('Input is not alphanumeric');
        return false;
    }

    return true;     
}
Barney
  • 16,181
  • 5
  • 62
  • 76
2
function isValidCharacter(txtTitle) {   
     var title = document.getElementById(txtTitle);
     var regExp = /^[a-zA-Z]*$/
     if (!regExp.test(title.value)) {
        title.value = '';
        return false;
        }
      else {      
           return true;
        }
   }


function Validation(){
 var txtTitles = document.getElementById('txtTitle');
  if (isValidCharacter(txtTitles.id) == false) {
   alert("Please enter valid title. No special character allowed.");        
    return false;
  }  
 }



   $("#Btn").unbind("click").click(function () {
        if (Validation() == false) {

        }
        else {
              //success     
        }
   }
ani
  • 111
  • 1
  • 5
  • 14
0

Input:

<input type="text" name="textname" id="tname"  onblur="namefun(this.value)">

Javascript

function namefun(c) {
  var spch = /[A-z\s]/gi;
  var dig = /[0-9]/g;
  var ln = c.length;
  var j = 1;
  for (var i = 0; i < ln; i++) {
    var k = c.slice(i, j);
    if (spch.test(c) == false || dig.test(c) == true) {
      alert("Invalid name");
      document.getElementById("tname").value = "";
      ln = 0;
      setTimeout(function () {
        tname.focus();
      }, 1);
      //return false;
    }
    j++;
  }
}
Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
Ann
  • 1
  • 1
0

Simple Javascript Version

isValidCharacter(text) {
  const validCharacterRegex = /^[a-zA-Z]*$/;
  return validCharacterRegex.test(text);
}

Typescript Version

isValidCharacter(text: string): boolean {
  const validCharacterRegex: RegExp = /^[a-zA-Z]*$/;
  return validCharacterRegex.test(text);
}
Nechar Joshi
  • 630
  • 1
  • 7
  • 14
0
<?php
     if(isset($_POST["Password"]))
            
   {
       
        $servername="localhost";
        $username="root";
        $password="";
        $dbname="coustomer";
           $conn = mysqli_connect($servername,$username,$password,$dbname);
           if(!$conn)
              {
                 die("connection failed:" .mysqli_connect_error());
              }
              $UserID= $_POST["Userid"];
              $Password= $_POST["Password"];

                $sql= "SELECT Name, Contact, Email, UserID, Password FROM coustomers WHERE UserID ='$UserID' password='$Password' ";
                 $result = $conn->query($sql);
                  if ($result->num_rows > 0) 
                {
                   while($row = $result->fetch_assoc())
                   {
                    echo '<script>alert("Login successfully")</script>';
                   }
                {
                     else
                        {
                         echo "No record found";
                         }
                           
   }  
?>
JochenJung
  • 7,183
  • 12
  • 64
  • 113
ankita
  • 1