Solved

Variables

Posted on 2000-03-14
19
217 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 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

15 Experts available now in Live!

Get 1:1 Help Now