LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2010, 01:56 PM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Checking input is Numerical


Hey guys, what i am trying to do is to allow the user to key in data , such as "23.23" or "24" , as it is the price of certain objects

How am i able to design a check which will allow me to prevent users from typing in input such as "ab.21" or "rfrr" as this field is purely a numerical field.

The problem i am facing is i tried using this search code.
Code:
echo "read this"
read this
if [ -z "`echo "$this" | tr -d  [[:digit:]]`" ] ;

echo "True - only  numeric"
else
echo "False - there are NON a numeric stuff here!"
fi
when i enter "1" or "a" for input, it will read the correct statements. but when i try inputting data such as "1.32" , it will give me a message saying "False - there are NON a numeric stuff here!". i can change the option to [[:alnum:]] , but then that would allow entries such as "aa.21" to be accepted.

Anybody knows how i can deal with this problem?
 
Old 01-18-2010, 02:09 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Try the following regular expression:

Code:
[0-9]+\.?[0-9]*
 
Old 01-18-2010, 02:23 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by gregarion View Post
Code:
echo "read this"
read this
if [ -z "`echo "$this" | tr -d  [.[:digit:]]`" ]
then
   echo "True - only  numeric"
else
   echo "False - there are NON a numeric stuff here!"
fi
include a full stop before the [:digit:]
 
Old 01-19-2010, 07:56 AM   #4
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Just to check. the code works by first echoing $this, and then deleting it only if the char in the "$this" is numerical or digit?
 
Old 01-19-2010, 08:02 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
in bash it can be done this way:
Code:
shopt -s extglob

echo "read this"
read this

if [[ $this == +([[:digit:]]) ]]; then    # +([[:digit:]])?(.+([[:digit:]]))
    echo "True - only  numeric"
else
    echo "False - there are NON a numeric stuff here!"
fi

Last edited by konsolebox; 01-19-2010 at 08:03 AM.
 
Old 01-19-2010, 08:03 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by devnull10 View Post
Try the following regular expression:

Code:
[0-9]+\.?[0-9]*
And what about scientific notation, i.e. 1.23e45 ? Or the OP doesn't need it ?
 
Old 01-19-2010, 08:14 AM   #7
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Nopes, it does not need scientific notation. Its just used to enter decimals meant for price of an object.

Sorry Konsolebox , but can i check why does "+([[:digit:]])" need an extra () and + infront of it? and what does "shopt -s extglob" do?

i tried running it like this :


Code:
#!/bin/bash



echo "read this"
read this

if [[ $this == +([[:digit:]]) ]]; then    # +([[:digit:]])?(.+([[:digit:]]))
    echo "True - only  numeric"
else
    echo "False - there are NON a numeric stuff here!"
fi

and i got this error message.


Code:
./test: line 8: syntax error in conditional expression: unexpected token `('
./test: line 8: syntax error near `+(['
./test: line 8: `if [[ $this == +([[:digit:]]) ]]; then    # +([[:digit:]])?(.+([[:digit:]]))'
is it due the fact i did not put in "shopt -s extglob"?
 
Old 01-19-2010, 08:27 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by gregarion View Post
is it due the fact i did not put in "shopt -s extglob"?
Yes. It enables extended globbing. Perhaps we can also do that we regex:
Code:
[[ $this =~ [[:digit:]]+(\.[[:digit:]]+)? ]] ...
Sorry I don't have access to a terminal to try this.
 
Old 01-19-2010, 08:32 AM   #9
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Hey thanks again konsolebox , but just to check with u about the code below

Code:
if [ -z $( echo "$this" | tr -d '[.[:digit:]]') ] ;

Code:
Code:

echo "$this" |
*this echo out the content inside $this and pipes it into the next set of command




Code:
Code:

tr -d '[.[:digit:]]')
*this command will check if the data from the $this is in digits and has a "." , and if it is, will delete them. if not, it will not delete them , thus the data will not be 0 and this the message that there is a non-numeric stuff is inside.


did i get it right?
 
Old 01-19-2010, 08:47 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I see nothing wrong with it but perhaps you need to quote $()?

Code:
if [ -z "$( echo "$this" | tr -d '[.[:digit:]]')" ]
You won't need it though if you're using '[[':
Code:
if [[ -z $(...) ]]; ...
Also I think that method is a bit.. dangerous or.. not reliable? It's even better if you just use grep:

Code:
 if echo "$this" | grep "^[[:digit:]]\+$" > /dev/null; then ...

Last edited by konsolebox; 01-19-2010 at 08:50 AM.
 
Old 01-19-2010, 09:15 AM   #11
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Lol, yeah, i am using the grep method, but i was asking about the other coding because i was curious about how it work for my general knowledge? is my explanation for it correct?
 
Old 01-19-2010, 10:54 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{
    printf "Read this: "
    getline this <"-"
    if ( this+0==this ){
        print "Ok"
    }else{
        print "Not ok"
    }
}'
output

Code:
$ ./shell.sh
Read this: 32121
Ok

$ ./shell.sh
Read this: .033
Ok

$ ./shell.sh
Read this: 231.cb
Not ok
 
1 members found this post helpful.
Old 01-19-2010, 11:27 PM   #13
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Could you explain how it works? i do not get the part +0?
 
Old 01-19-2010, 11:39 PM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gregarion View Post
Could you explain how it works? i do not get the part +0?
this+0 converts it to integer(number)
 
Old 01-20-2010, 08:12 PM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
An invalid input like this can also make a match.
Code:
this="1.2.3.4....."
echo "$this" | tr -d '[.[:digit:]]'
It's really better if you just use grep or any other than tr.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking Input gregarion Programming 6 01-14-2010 08:04 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
Checking numerical data!! (java) Fredstar Programming 2 03-17-2006 07:42 AM
Repeatedly checking for input kamransoomro84 Programming 16 05-26-2004 03:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration