Forum: Ruby Basic Question on passing value to Javascript

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
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
This topic is locked and can not be replied to.