Solved

Variables

Posted on 2000-03-14
19
218 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
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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now