Solved

Variables

Posted on 2000-03-14
19
228 Views
Last Modified: 2013-12-25
I've got some variables that have to be filled in in a form. Now I want to check the correctness of the filled in variables, by checking them on having :

a) both numbers and alphabetic writing  
b) numbers only
c) alphabetic writing only
d) a @ character

I do not want certain input to be filled in at certain questions of the form (I want to exclude certain combinations of characters at some questions). Can you provide me with the lines to check the correctness of the variables filled in ?
Thanks
0
Comment
Question by:quickbasic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
19 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2617195
if you prefer using a faster validation process try using javascript. That way you dont have to go back-and-forth between your server and browser to validate every column.

here is a generic & flexible javascript code that uses a validation mask to see it that particular field has list of "good/permissible" characters.

if it does it show an alert box with a custom message.

in the foll eg. the usename can ONLY have alphabets (capital and small), numbers & - _ and . in it.

Also password can have alphabets (capital and small), numbers  and ~!@#$%^&*()_+=-:;,.?  in it.

=================validate.html
<script Language="JavaScript">
<!--
function Form_Validator(theForm){
  // Validate the First Name
  var checkStr = theForm.user.value;
  if (checkStr == ""){
    alert("Please enter a value for the \"User Name\" field.");
    theForm.user.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
  var allValid = true;

  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
      for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
          break;
      if (j == checkOK.length){
        allValid = false;
        break;
      }
    }
     
    if (!allValid){
      alert("Please enter only letter, numbers , - . and _ characters in the \"User Name\" field.");
      theForm.user.focus();
      return (false);
    }

  // Validate the Password
  var checkStr = theForm.pass.value;
  if (checkStr == ""){
    alert("Please enter a value for the \"Password\" field.");
    theForm.pass.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=-:;,.?";
  var allValid = true;

  for (i = 0;  i < checkStr.length;  i++){
    ch = checkStr.charAt(i);
      for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
          break;
      if (j == checkOK.length){
        allValid = false;
        break;
      }
    }
     
  if (!allValid)
  {
    alert("Please enter only letter, numbers, and other special characters in the \"Password\" field.");
    theForm.pass.focus();
    return (false);
  }
  return (true);
}
//-->
</script>

<FORM name="thisForm" action="/scripts/newscripts/login.pl" method="post"
 onsubmit="return Form_Validator(this)">      
<TABLE cellspacing=0 cellpadding=0 border=0>
<TR><TD align=left><FONT face="Arial, Helvetica" size=2 color="#000000">&nbsp;User Name</FONT></TD></TR>
<TR><TD valign=center align=center><input size="14" maxlength="30" name="user"></TD></TR>
<TR><TD valign=center align=left><FONT face="Arial, Helvetica" size=2 color="#000000">Password</FONT></TD></TR>
<TR><TD align=center><input type="password" size="14" maxlength="30" name="pass"></TD></TR>
<TR><TD align=center width=100%><FONT face="Arial, Helvetica" size=1><BR></FONT><input type="submit" name="Enter" value="Login"></TD></TR>
</TABLE>
</FORM></BODY></HTML>
0
 

Expert Comment

by:oostwijk
ID: 2617662
I'm sorry, I prefer using a cgi script.
0
 

Author Comment

by:quickbasic
ID: 2617967
Adjusted points from 50 to 75
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:quickbasic
ID: 2620616
I've got some variables that have to be filled in in a form. Now I want to check the correctness of the filled in variables, by checking them on having :

a) both numbers and alphabetic writing  
b) numbers only
c) alphabetic writing only
d) a @ character
e) And I want a line that checks how much characters the variable has got.

Can you provide me with the lines to check the correctness of the variables filled in ?
Thanks
0
 
LVL 1

Expert Comment

by:lambda
ID: 2622250

#For alphabet-number validation,
if(!($variable =~ /^\w(\w+)?$/g))
{
  print "invalid character";
}
else
{
  print "valid character";
}

# For @ validation,
if (!($variable =~ /\@/g))
{
      print "invalid";
}
else
{
      print "valid";
}

0
 
LVL 1

Expert Comment

by:lambda
ID: 2622257

# For number validation,

if (($variable =~ /\D/g))
{
      print "invalid";
}
else
{
      print "valid";
}

0
 
LVL 1

Expert Comment

by:lambda
ID: 2622343

#For finding no of characters in a variable,

$len = length $variable;
print $len;
0
 
LVL 1

Expert Comment

by:lambda
ID: 2622363

## For alphabet only checking...

if (($variable =~ /^[a-zA-Z]/g))
{
      print "has alpha alone";
}
else
{
      print "not only alpha";
}
0
 
LVL 1

Expert Comment

by:lambda
ID: 2628791

Was ur problem solved.....?
0
 

Author Comment

by:quickbasic
ID: 2634272
Still got to check it out, I was away this weekend.
0
 

Author Comment

by:quickbasic
ID: 2637815
While testing the script I encountered the following problems in the checking of:
- Alphabet+number validation.
  When I enter a alphabet/number combination I get the message 'Invalid character'. I get this message also with alphabet variables.
- @ validation.
  I'm getting an Error 500 when I try to use 'hello@hotmail.com' as a variable. I've come to the conclusion that these lines work when there are characters in front of the '@' but that it doesn't work when there are characters behind the '@'.
- Alphabet only checking
  I found that these lines only check on the first character of the variable.
- Number validation
  This works fine, but I forgot to mention that I also want that amounts with decimals will be accepted.
0
 
LVL 1

Expert Comment

by:lambda
ID: 2638803
The alphabet+number validation did work for me.

$variable = "abc123"; # prints valid character

if(!($variable =~ /^\w(\w+)?$/g))
{
  print "invalid character";
}
else
{
  print "valid character";
}

#For @ validations, you have to escape with a \.

$variable="abc\@hotmail.com";

if (!($variable =~ /\@/g))
{
print "invalid";
}
else
{
print "valid";
}

U are right, there was a bug in the alphabet-only validation. I'll just verify it & let you know. Meanwhile please check the above ones and tell me.

0
 
LVL 1

Expert Comment

by:lambda
ID: 2638910
# For alpha only vaidation ...

$variable = "AabAAcdef";

if (($variable =~ /\w/) && !($variable =~ /\d|\_|\./g))
{
      print "alpha alone";
}
else
{
      print "not only alpha";
}
0
 
LVL 1

Expert Comment

by:lambda
ID: 2639650
Ooops! Bug again!
Change the if condition to

if (($variable =~ /^\w(\w+)?$/g) && !($variable =~ /\d|\_/))

Sorry !!
0
 

Author Comment

by:quickbasic
ID: 2650375
still got to check it out... You'll here from me within a couple of days..
0
 

Author Comment

by:quickbasic
ID: 2656375
- If a user filles in his e-mail adress in a form does the @-validation check it correctly ? cause then it is not escaped with a \.

- Number validation
  This works fine, but as I mentioned I also want that amounts with decimals will be accepted.
0
 
LVL 1

Expert Comment

by:lambda
ID: 2656532

If you are reading email address from the HTML, you needn't escape the @. Escape it only if you're directly giving the value to the variable.

I am writing this from home and I don't have access to our server... so I can't check the number validation. Will let you know soon though.
0
 
LVL 1

Accepted Solution

by:
lambda earned 75 total points
ID: 2660222
Here goes...

# Number & decimal validation...

$num = "1.123";

if (($num =~ /^\d(\d+)?$/) || ($num =~ /^(\d+)?(\.)?(\d+)$/))
{
      print "has number only";
}
else
{
      print "not only number";
}

Try this.
0
 

Author Comment

by:quickbasic
ID: 2661429
Excellent, this will do the job... tnx
0

Featured Post

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question