Validation problem

Hello G.s
i want to use external validation i mean javascript validation but
whenever click on create i mean (add new record submit)that time my
first text box is black then it give me alert but when i click on ‘ok’
it redirect to controller and give error not focus on same text box.

Wap A. wrote:

Hello G.s
i want to use external validation i mean javascript validation but
whenever click on create i mean (add new record submit)that time my
first text box is black then it give me alert but when i click on ‘ok’
it redirect to controller and give error not focus on same text box.

Whoa, take a step back here and slow down so that we can understand you.
That’s one of the longest run-on sentences I’ve seen in my life.

You want to use javascript validation because you are using something
like a form_tag correct? Which tag are you using to start with?

Start with providing the basics of what you have, show some initial code
for the piece you are having issues with.

You do not have to use javascript validation on external pieces. I have
created custom validation methods for pieces like form_tag, etc.

The larger question is you mentioned a create or a submit to add a
record, which tells me that you are adding it into a database. If that
is the case then your validations should be performed in the model, not
with javascript.

I am using form for here is my code
new.html.erb

New animal

<% form_for(@animal) do |f| %>
<%= f.error_messages %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :desc %>
<%= f.text_field :desc %>

<%= f.submit 'Create', :onClick => 'new_animal();' %>

<% end %>
<%= link_to ‘Back’, animals_path %>

main.js

function new_animal(){
alert(‘animal’);
if(document.getElementById(‘animal_name’).value=="")
{
alert(“Enter name…”);
document.getElementById(‘animal_name’).focus();
return false;
}
if(document.getElementById(‘animal_desc’).value=="")
{
alert(“Enter animal description …”);
document.getElementById(‘animal_desc’).focus();
return false;
}
}

i got message that animal then i preesed ok form redirt to index page
and insert record in db.

Why are you doing this in javascript when you can do this beautifully
in your controller? Create the appropriate validations in your model
and then in your controller check whether saving is possible. If it
is, save it and redirect or whatever you want it to do. If not, catch
the error and show it.

jhaagmans wrote:

Why are you doing this in javascript when you can do this beautifully
in your controller? Create the appropriate validations in your model
and then in your controller check whether saving is possible. If it
is, save it and redirect or whatever you want it to do. If not, catch
the error and show it.

ok dear but this is server side validation. i want to use client side
validate.

server side take more time execution so that client side validation more
fast. so that i want to use external js validation.

jhaagmans wrote:

Why are you doing this in javascript when you can do this beautifully
in your controller? Create the appropriate validations in your model
and then in your controller check whether saving is possible. If it
is, save it and redirect or whatever you want it to do. If not, catch
the error and show it.

ok but as per my above validation code is not possible ?

On Aug 22, 10:43 am, Wap A. [email protected]
wrote:

server side take more time execution so that client side validation more
fast. so that i want to use external js validation.

I hope you are also doing server side validation. This is better done
from the form’s onsubmit, and don’t forget to return the value of your
validation function (don’t just call it as you are doing now)

Fred

Don’t think like this - as other posters have pointed out, you’re
going to have to do server-side validation anyways. Worrying about the
execution time of something this small is exceptionally premature
optimization.

In your example, the JS validation may work, but you’re still going to
have to validate server-side, as some users may have JS turned off.

–Matt J.

On Aug 22, 5:43 am, Wap A. [email protected]

Matt J. wrote:

Don’t think like this - as other posters have pointed out, you’re
going to have to do server-side validation anyways. Worrying about the
execution time of something this small is exceptionally premature
optimization.

In your example, the JS validation may work, but you’re still going to
have to validate server-side, as some users may have JS turned off.

–Matt J.

On Aug 22, 5:43�am, Wap A. [email protected]

ok dear that’s true

Wap A. wrote:

Matt J. wrote:

Don’t think like this - as other posters have pointed out, you’re
going to have to do server-side validation anyways. Worrying about the
execution time of something this small is exceptionally premature
optimization.

In your example, the JS validation may work, but you’re still going to
have to validate server-side, as some users may have JS turned off.

–Matt J.

On Aug 22, 5:43�am, Wap A. [email protected]

ok dear that’s true

Use Live Validation.

<%= f.submit ‘Create’, :onClick => ‘new_animal()’ %>

function new_animal(){
alert(‘animal’);
if(document.getElementById(‘animal_name’).value==“”)
{
alert(“Enter name…”);
document.getElementById(‘animal_name’).focus();
return false;
}
if(document.getElementById(‘animal_desc’).value==“”)
{
alert(“Enter animal description …”);
document.getElementById(‘animal_desc’).focus();
return false;
}
return true;
}

hope it will run…

hope it will run…
sorry this one no working …