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
Solved

Variables

Posted on 2000-03-14
19
220 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

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…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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