1

I have adding dynamic row using Javascript in php. I need to check the validation. I am using onsubmit for validation I have got the error message when am not entering the value, if am entering the value is not post and not fetch the values also.
Example code:

<form name="myForm" method="post" action="" onsubmit="return validate();">  
var row = table.insertRow(2).outerHTML="<tr id='row"+table_len+"'><td></td><td id='pickup"+table_len+"'><input type='text'  id='pickup' name='pickup' onkeypress='return blockSpecialChar(event,this)'  class='myTextBox1' onblur='UpperCase(this)' maxlength='8'  value='' style='width:70px;text-align:left;'   required></td><td> <input type='submit' class='center' style='margin-left: 8px;align:center;padding: 1px 4px;' id='submit"+table_len+"' name='submit' value='Save' class='save' > <input type='button' style='margin-left: 6px;align:center;padding: 1px 7px;' value='Cancel'  class='center' onclick='cancel_row_add("+table_len+")'></td></tr><br>";
<script type="text/javascript">
function Validate()
{
    if(document.getElementById('pickup').value == '')
    {
    alert('Please enter pickup Name');
    document.getElementById('pickup').focus();
    return false;
    }
}
</script>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
maheswari
  • 19
  • 2
  • 1
    Your function Validate has the letter V capitalized, but on the submit its all lowercase – Kevin.a May 30 '18 at 07:11
  • 3
    `var row =....` seems a javascript code running outside `` . Is your code really working? Please add full code not chunks of it. Chunks made your question unclear. Also `validate() !==Validate()`. `v` word have problem there(lowercase-uppercase) – Alive to die - Anant May 30 '18 at 07:11

2 Answers2

0

Javascript functions are case sensitive.

Change validation() to Validation() in onsubmit

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
PTak
  • 62
  • 5
0

For starters. Change:

<form name="myForm" method="post" action="" onsubmit="return validate();">

to

<form name="myForm" method="post" action="" onsubmit="return Validate();">

I don't know what you're trying here: var row = table.insertRow(2).outerHTML="<tr id='row"+table_len+"'> // .... code But it seems like you're trying to run JS code outside <script> tags. Move it into the <script>. Since you're trying to get the ID pickup in your Validate() function.

Maybe try adding an ID to your form. like: id="myForm" and try an onclick event (and remove the onsubmit attribute):

$("#myForm").on('click', function(){
    alert('Please enter pickup Name');
    document.getElementById('pickup').focus();
    return false;
});

Add an ID to your submit button e.g.: <input type="button" id="formSubmitBtn"> and try this:

$(document).ready(function(){
    function Validate(){
        if(document.getElementById('pickup').value == '')
        {
            alert('Please enter pickup Name');
            document.getElementById('pickup').focus();
            return false;
        }
    }
    $("#formSubmitBtn").on('click',Validate);
}); 

And if all of this doesn't work. Open the developer console by pressing F12 and clicking console. Click on the submit button and see what kind of JS Errors you get.

Rick J
  • 166
  • 1
  • 15