Solved

Variables

Posted on 2000-03-14
19
225 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

710 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