# validate input

**URL:** <https://community.unix.com/t/validate-input/230616>\
**Category:** Shell Programming and Scripting\
**Created:** [February 6, 2009, 11:23am UTC](https://community.unix.com/t/validate-input/230616 "2009-02-06T11:23:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DDoS](https://community.unix.com/user_avatar/community.unix.com/ddos/32/4805_2.png) [@DDoS](https://community.unix.com/u/DDoS)\
**Post date:** [February 6, 2009, 11:23am UTC](https://community.unix.com/t/validate-input/230616/1 "2009-02-06T11:23:09Z")

</div>

the user inputs names that have to be inside square brackets  
[John,Dan]  
I want to check if the user puts the brackets and if not ask him to re-enter the names

---

<div class="post-metadata">

**Author:** ![quirkasaurus](https://community.unix.com/user_avatar/community.unix.com/quirkasaurus/32/1325_2.png) [@quirkasaurus](https://community.unix.com/u/quirkasaurus)\
**Post date:** [February 6, 2009, 11:46am UTC](https://community.unix.com/t/validate-input/230616/2 "2009-02-06T11:46:00Z")

</div>

why not just add the brackets yourself? it's far easier...

anyhoo....

```nohighlight
#!/bin/ksh

input='[alskhdf]'

case $input in

  \[*\]) echo good ;;
  *) echo bad ;;

esac

```

---

<div class="post-metadata">

**Author:** ![joeyg](https://community.unix.com/user_avatar/community.unix.com/joeyg/32/780_2.png) [@joeyg](https://community.unix.com/u/joeyg)\
**Post date:** [February 6, 2009, 12:10pm UTC](https://community.unix.com/t/validate-input/230616/3 "2009-02-06T12:10:55Z")

</div>

```nohighlight
> invalue="[John,Dan,]"
> if [`echo "${invalue}" | tr "[:alpha:]" "~" | tr -s "~"` != "[~,~]" ] ; then echo "Bad input format; should be [name1,name2]"; fi
Bad input format; should be [name1,name2]

> invalue="[John,Dan]"
> if [`echo "${invalue}" | tr "[:alpha:]" "~" | tr -s "~"` != "[~,~]" ] ; then echo "Bad input format; should be [name1,name2]"; fi
> 

```

---

<div class="post-metadata">

**Author:** ![Shahul](https://community.unix.com/user_avatar/community.unix.com/shahul/32/1043_2.png) [@Shahul](https://community.unix.com/u/Shahul)\
**Post date:** [February 6, 2009, 12:54pm UTC](https://community.unix.com/t/validate-input/230616/4 "2009-02-06T12:54:37Z")

</div>

Hope this also should work 🙂

#!/bin/ksh

eval input=$@

echo $input |awk '/^\[/ {print}'  
if [$? -eq 0]  
then  
echo $input |awk '/\]$/ {print}'  
if [$? -eq 0]  
then  
echo "good"  
else  
echo "Please enter the input ending with ]"  
fi  
else  
echo "Please enter the input starting with ["  
fi

---

<div class="post-metadata">

**Author:** ![DDoS](https://community.unix.com/user_avatar/community.unix.com/ddos/32/4805_2.png) [@DDoS](https://community.unix.com/u/DDoS)\
**Post date:** [February 6, 2009, 3:27pm UTC](https://community.unix.com/t/validate-input/230616/5 "2009-02-06T15:27:01Z")

</div>

The solution are working guys but i want it to be like a loop.The user cannot exit unless he enters the correct input  
Thanks anw

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/5_5575768a8748004e209b776fc1b2916d.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [February 6, 2009, 3:51pm UTC](https://community.unix.com/t/validate-input/230616/6 "2009-02-06T15:51:54Z")

</div>

> [@ddos](#):
>
> The solution are working guys but i want it to be like a loop.The user cannot exit unless he enters the correct input  
> Thanks anw

That's another story and you should search the forum and if you don't find the answer start a new thread.

---

<div class="post-metadata">

**Author:** ![ddreggors](https://community.unix.com/user_avatar/community.unix.com/ddreggors/32/1064_2.png) [@ddreggors](https://community.unix.com/u/ddreggors)\
**Post date:** [February 7, 2009, 1:50am UTC](https://community.unix.com/t/validate-input/230616/7 "2009-02-07T01:50:49Z")

</div>

Try this:

```nohighlight
eval input=$@
VALID="false"
while ["$VALID" != "true"]
do
    echo $input |awk '/^\[/ {print}'
    if [$? -eq 0]
    then
        echo $input |awk '/\]$/ {print}'
        if [$? -eq 0]
        then
            echo "good"
            VALID="true"
        else
            echo "Please enter the input ending with ]"
            VALID="false"
        fi
    else
        echo "Please enter the input starting with ["
        VALID="false"
    fi
done

```

---

<div class="post-metadata">

**Author:** ![DDoS](https://community.unix.com/user_avatar/community.unix.com/ddos/32/4805_2.png) [@DDoS](https://community.unix.com/u/DDoS)\
**Post date:** [February 7, 2009, 4:14pm UTC](https://community.unix.com/t/validate-input/230616/8 "2009-02-07T16:14:16Z")

</div>

I have this code.The case works but i get the following error  
for while: [:missing `]'

VALID="false"  
while ["$VALID" != "true"]  
do  
case $input in  
\[\*\]) echo Input is ok  
VALID="true";;  
\*) echo Invalid input  
echo Enter input again in [,]  
read input  
VALID="false";;  
esac  
done

---

<div class="post-metadata">

**Author:** ![ddreggors](https://community.unix.com/user_avatar/community.unix.com/ddreggors/32/1064_2.png) [@ddreggors](https://community.unix.com/u/ddreggors)\
**Post date:** [February 7, 2009, 4:52pm UTC](https://community.unix.com/t/validate-input/230616/9 "2009-02-07T16:52:53Z")

</div>

> [@ddos](#):
>
> while ["$VALID" != "true"]

Insert spaces between the brackets ( [and] ) and the variables:

```plaintext
while ["$VALID" != "true"]

```

---

<div class="post-metadata">

**Author:** ![DDoS](https://community.unix.com/user_avatar/community.unix.com/ddos/32/4805_2.png) [@DDoS](https://community.unix.com/u/DDoS)\
**Post date:** [February 7, 2009, 5:21pm UTC](https://community.unix.com/t/validate-input/230616/10 "2009-02-07T17:21:11Z")

</div>

it works fine! thank u very much!!😛
