Custom validation rule for tilde and skew in PHP and JQuery

I need good validation for tild (~) and tilt (`) characters in PHP and JQuery. Here is a JQuery confirmation that I did.

$.validator.addMethod("validSMS", function(value, element) {
    if (value.indexOf('~') === -1 || value.indexOf('`') === -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}, "Tild and tilt Character is not allowed");

      

But I need another good method for this validation in JQuery and PHP.

+3


source to share


2 answers


This is the php version using strpos



if (strrpos($mystring, "~") === false && strrpos($mystring, "`") === false ) { 
    // $mystring has no tild(~) and tilt(`) chars
}

      

0


source


this is something that can be done with javascript. you can change the condition as per your requirement.



<script language=javascript>
  var txt='jayd~ev';

  var re1='.*?';    // Non-greedy match on filler
  var re2='(~)';    // Any Single Character 1

  var p = new RegExp(re1+re2,["i"]);
  var m = p.exec(txt);
  if (m != null)
  {
      var c1=m[1];
      document.write("("+c1.replace(/</,"&lt;")+")"+"\n");
  }
</script>

      

0


source







All Articles