in reply to String Validation

The validation I need is

if $string has anything other than A-Z or 0-9 - PASS
else FAIL.

Replies are listed 'Best First'.
Re^2: String Validation
by ikegami (Patriarch) on Mar 07, 2007 at 19:18 UTC
    if ($string =~ /[^A-Z0-9]/) { # String contains a character other than A-Z, 0-9. ... } else { # String only contains characters A-Z, 0-9. ... }

    or

    if ($string =~ /^[A-Z0-9]*\z/) { # String only contains characters A-Z, 0-9. ... } else { # String contains a character other than A-Z, 0-9. ... }
Re^2: String Validation
by shigetsu (Hermit) on Mar 07, 2007 at 19:28 UTC
    #!/usr/bin/perl -l use strict; use warnings; my $regexp = qr/^[^A-Z0-9]+$/; print 'abc' =~ $regexp; print 'ABC' =~ $regexp; print 'ab0' =~ $regexp; print '^|+' =~ $regexp; print '' =~ $regexp;
    prints
    1 (0) (0) 1 (0)