0

This is my onsubmit function where i have problem. Hope you guys will help.

function editme() {
  var peru=document.editform.user_nam.value;  
  var mailu=document.editform.user_mai.value; 

if (peru==null || peru==""){  
  alert("Name can't be blank");  
  return false;  
}

if(!/(?=.{0,20}$)\S+\s\S+/.test(peru)){                                   
  document.getElementById("eredit").innerHTML="1.Value entered in the Name  field is invalid;"
}

if(!/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/.test(mailu)){
  document.getElementById("erredit").innerHTML="2.Value entered in the E-mail field is invalid";
}

 var aa=document.getElementById("ename").value;
 var bb=document.getElementById("email").value;
 var cc=document.getElementById("sele").value;
 var tab=document.getElementById("myTable");
 tab.rows[ind].cells[1].innerHTML=aa;
 tab.rows[ind].cells[2].innerHTML=bb;
 tab.rows[ind].cells[3].innerHTML=cc;
}

Here, even if there are any validation messages, my values are getting submitted to a table.I want to stop the function where there are error messages. How to do that?

  • 1
    Possible duplicate of [How to prevent form from being submitted?](https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) – Björn Tantau Aug 11 '17 at 08:49

1 Answers1

0

Have you tried adding return false, in to the both ifs? Like this:

if(!/(?=.{0,20}$)\S+\s\S+/.test(peru)){                                   
    document.getElementById("eredit").innerHTML="1.Value entered in the Name  field is invalid;"
    return false;
}

if(!/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/.test(mailu)){
    document.getElementById("erredit").innerHTML="2.Value entered in the E-mail field is invalid";
    return false;
}

Also would be much helpful if you can post your whole code, so I can replicate the problem to see and work with it.

  • thanks a lot.return false solved my prob.I wouls have posted whole code it is a lengthy code.so i didnt post it.Thanks again – VENKATTHERAISINGSTAR Aug 11 '17 at 08:58
  • No problem man. You can select this answer as "accepted", as this question doesn't need more answering. Keep on coding and mostly have fun ! :) gl – Jozef Maloch Aug 11 '17 at 09:01
  • how to make it accepted i am new to stackoverflow and i dont even have points. – VENKATTHERAISINGSTAR Aug 11 '17 at 09:16
  • can i ask one more question if..its okay – VENKATTHERAISINGSTAR Aug 11 '17 at 09:18
  • https://stackoverflow.com/help/someone-answers and in the top right there is a question mark icon, click on it and then click on tour and just read stuff about SO there, also https://stackoverflow.com/help/how-to-ask is good to read, and yes ask anything u want mate :) – Jozef Maloch Aug 11 '17 at 09:20
  • if(!/\S+\s\S+/.test(name)) using this if condition i tried to create a single space between two words.But for as u can see above by using thiscondition if(!/(?=.{0,20}$)\S+\s\S+/.test(peru)) i tried to limit the size but once i do this it is accepting the first letter even if it is lowercase which shd not happen ....if u knoe something help me out – VENKATTHERAISINGSTAR Aug 11 '17 at 09:21
  • I am afraid I can't help u with this one, I am not very good with RegExp, try asking on SO :) – Jozef Maloch Aug 11 '17 at 09:37
  • i tried but its kind of showing similar questions found..but i couldnt find any...i am also not very good at regular expressions – VENKATTHERAISINGSTAR Aug 11 '17 at 10:01