Forum: Ruby Basic Question on passing value to Javascript

Ef77f58090c3abe70b827a8b21e5ec74?d=identicon&s=25 Adarsh Mehrotra (adarsh12in)
on 2014-06-18 07:29
Hi

I am fairly new to Ruby (& Sinatra). I have a web form (ERB) where none
of the fields can be blank and I need client side validation to show
fields are empty.

I want to get the value from text box field and pass to javascript to
find out if it is empty. As I realize, server side is getting mixed up
with client side and i am not getting the value passed  correctly.

Here is my code-- Can someone please guide (apologize the query is
trivial)

Form1.erb--

<tr>
          <td> <label for="txtHost"><h4>Host</h4></label></td>
          <td><input type="text" id="txtHost" name="txtHost"></td>
        </tr>
        <@str = txtHost.value>

<script language="javascript">
    function validateForm() {
          var x = '<%= @str %>'
        if (x == null || x == "") {
            alert("Host must be filled out");
            return false;
            }
        }
9fa32e40121025416cb389b0f5b69cbb?d=identicon&s=25 Benjamin Klotz (benny1992)
on 2014-06-18 13:49
Maybe this could help you - https://github.com/gazay/gon-sinatra

Greetings Benny
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.